Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在PHP类上定义常量吗?_Php_Constants_Class Constants - Fatal编程技术网

我可以在PHP类上定义常量吗?

我可以在PHP类上定义常量吗?,php,constants,class-constants,Php,Constants,Class Constants,我在一些类上定义了几个常量,希望得到它们的列表。例如: class Profile { const LABEL_FIRST_NAME = "First Name"; const LABEL_LAST_NAME = "Last Name"; const LABEL_COMPANY_NAME = "Company"; } 有没有办法获取在Profile类上定义的CONST的列表?据我所知,最接近的optionget_定义的_常量不会起作用 我实际上需要的是一个常量名称列表—

我在一些类上定义了几个常量,希望得到它们的列表。例如:

class Profile {
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";
}
有没有办法获取在Profile类上定义的CONST的列表?据我所知,最接近的optionget_定义的_常量不会起作用

我实际上需要的是一个常量名称列表——类似这样:

array('LABEL_FIRST_NAME',
    'LABEL_LAST_NAME',
    'LABEL_COMPANY_NAME')
或:

甚至:

array('Profile::LABEL_FIRST_NAME'=>'First Name', 
    'Profile::LABEL_LAST_NAME'=>'Last Name',
    'Profile::LABEL_COMPANY_NAME'=>'Company')
你可以用这个。请注意,如果您经常这样做,您可能希望查看缓存结果

<?php
class Profile {
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";
}


$refl = new ReflectionClass('Profile');
print_r($refl->getConstants());
你可以用这个。请注意,如果您经常这样做,您可能希望查看缓存结果

<?php
class Profile {
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";
}


$refl = new ReflectionClass('Profile');
print_r($refl->getConstants());
是的,你用。看一下

<?
Reflection::export(new ReflectionClass('YourClass'));
?>
这会让你知道你要看什么。

是的,你可以使用。看一下

<?
Reflection::export(new ReflectionClass('YourClass'));
?>

这应该会让您了解您将看到的内容。

在PHP5中,您可以使用反射:


在PHP5中,可以使用反射:

使用。即:

使用。即:

使用ReflectionClass并提供您想要的:

<?php
class Cl {
    const AAA = 1;
    const BBB = 2;
}
$r = new ReflectionClass('Cl');
print_r($r->getConstants());
使用ReflectionClass并提供您想要的:

<?php
class Cl {
    const AAA = 1;
    const BBB = 2;
}
$r = new ReflectionClass('Cl');
print_r($r->getConstants());

根据PHP文档注释,如果您能够使用ReflectionClass PHP 5:

function GetClassConstants($sClassName) {
    $oClass = new ReflectionClass($sClassName);
    return $oClass->getConstants();
}

根据PHP文档注释,如果您能够使用ReflectionClass PHP 5:

function GetClassConstants($sClassName) {
    $oClass = new ReflectionClass($sClassName);
    return $oClass->getConstants();
}

为什么不把它们作为数组放在类变量中呢?使循环通过更容易

private $_data = array("production"=>0 ...);

为什么不把它们作为数组放在类变量中呢?使循环通过更容易

private $_data = array("production"=>0 ...);

最终使用名称空间:

namespaces enums;
class enumCountries 
{
  const CountryAustria          = 1 ;
  const CountrySweden           = 24;
  const CountryUnitedKingdom    = 25;
}

最终使用名称空间:

namespaces enums;
class enumCountries 
{
  const CountryAustria          = 1 ;
  const CountrySweden           = 24;
  const CountryUnitedKingdom    = 25;
}

在类中有一个方法可以方便地返回自己的常量。 您可以这样做:

class Profile {
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";


    public static function getAllConsts() {
        return (new ReflectionClass(get_class()))->getConstants();
    }
}

// test
print_r(Profile::getAllConsts());

在类中有一个方法可以方便地返回自己的常量。 您可以这样做:

class Profile {
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";


    public static function getAllConsts() {
        return (new ReflectionClass(get_class()))->getConstants();
    }
}

// test
print_r(Profile::getAllConsts());
用静态的方法-来拯救 看起来这是一个将Traits与静态函数结合使用来扩展类功能的好地方。Traits还允许我们在任何其他类中实现此功能,而无需反复重写相同的代码

在Profile类中使用自定义的“ConstantExport”特性。为每个需要此功能的类执行此操作

/**
 * ConstantExport Trait implements getConstants() method which allows 
 * to return class constant as an assosiative array
 */
Trait ConstantExport 
{
    /**
     * @return [const_name => 'value', ...]
     */
    static function getConstants(){
        $refl = new \ReflectionClass(__CLASS__);
        return $refl->getConstants();
    }
}

Class Profile 
{
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";

    use ConstantExport;

}
举例

// So simple and so clean
$constList = Profile::getConstants(); 

print_r($constList); // TEST
产出:

Array
(
    [LABEL_FIRST_NAME] => First Name
    [LABEL_LAST_NAME] => Last Name
    [LABEL_COMPANY_NAME] => Company
)
用静态的方法-来拯救 看起来这是一个将Traits与静态函数结合使用来扩展类功能的好地方。Traits还允许我们在任何其他类中实现此功能,而无需反复重写相同的代码

在Profile类中使用自定义的“ConstantExport”特性。为每个需要此功能的类执行此操作

/**
 * ConstantExport Trait implements getConstants() method which allows 
 * to return class constant as an assosiative array
 */
Trait ConstantExport 
{
    /**
     * @return [const_name => 'value', ...]
     */
    static function getConstants(){
        $refl = new \ReflectionClass(__CLASS__);
        return $refl->getConstants();
    }
}

Class Profile 
{
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";

    use ConstantExport;

}
举例

// So simple and so clean
$constList = Profile::getConstants(); 

print_r($constList); // TEST
产出:

Array
(
    [LABEL_FIRST_NAME] => First Name
    [LABEL_LAST_NAME] => Last Name
    [LABEL_COMPANY_NAME] => Company
)

+1,尽管我想说这是一个使用其他海报中提到的反射的绝佳时机,但了解引擎盖下的工作原理并能够在没有它们的情况下工作或在必要时复制它们也很重要。很好的展示。如果你不想你的类被加载到内存中,token_get_all是一个不错的选择。它比反射快得多,如果需要使用大量的类,它不会使进程内存混乱。+1用于基于令牌的解决方案!考虑到性能,理解基于令牌的解析是一种乐趣。。。像往常一样,有一个很棒的人展示了如何通过token_get_all解析常量。非常感谢你!这可能只作用于单个文件,不从父类继承任何常量。事实上,这种技术甚至不关心类——它将为您提供文件中的所有常量,甚至是全局范围内的常量。这是一个很好的探索工具。+1,尽管我想说这是一个很好的使用反射的时间,正如其他海报所提到的,理解引擎盖下的工作原理也很重要,并且能够不使用它们,或者在必要时复制它们。很好的展示。如果你不想你的类被加载到内存中,token_get_all是一个不错的选择。它比反射快得多,如果需要使用大量的类,它不会使进程内存混乱。+1用于基于令牌的解决方案!考虑到性能,理解基于令牌的解析是一种乐趣。。。像往常一样,有一个很棒的人展示了如何通过token_get_all解析常量。非常感谢你!这可能只作用于单个文件,不从父类继承任何常量。事实上,这种技术甚至不关心类——它将为您提供文件中的所有常量,甚至是全局范围内的常量。这是一个值得探索的好工具。+1这就是它,因为我找不到任何用于获取类常量的内置过程PHP函数,这有点遗憾。可能是因为不需要它。OP可能希望通过将类型设置为该类具有的所有常量来进行元配置,在大多数情况下,在我有限的意见中,继承或静态数组变量可能会更好地满足这一要求,类型为常量留出空间

这与2009年发布的被接受的答案有什么不同?@billynoah:对于那些后来被标记为重复的问题,如果你没有意识到,那么这些旧问题往往会被合并:你可能会遇到更多这样的问题。这确实与公认的答案没有什么不同。然而,在某个时间点上,它可能是对一个现已被删除为重复问题的公认答案。@Wrikken-这很有意义。感谢您解释+1,这就够了,因为我找不到任何用于获取类常量的内置过程PHP函数,这有点遗憾。可能是因为不需要它。OP可能希望通过将类型设置为该类的所有常量来进行元配置,在大多数情况下,在我有限的意见中,继承或静态数组变量的类型为具有其他含义/用途的常量留有空间。这与2009年发布的公认答案有什么不同?@billynoah:关于后来被标记为重复的问题的答案,如果你没有意识到,那么这些旧的问题往往会被合并:你可能会遇到更多这样的情况。这确实与公认的答案没有什么不同。然而,在某个时间点上,它可能是对一个现已被删除为重复问题的公认答案。@Wrikken-这很有意义。感谢您解释两个次要的NB:首先,在5.3中,Profile可以用作reflector构造函数的参数,不需要引用简单的类名;其次,完全清楚地说,结果数组的键是字符串,而不是此处的格式可能暗示的常量。仅值得一提的是fn是。@5.3中的Benji XVI如果打开了通知,您将无法使用没有引号的配置文件,因为它将显示以下错误:注意:使用未定义的常量配置文件-假定为“配置文件”。所以我建议保留引号'Profile',最好在类内部定义与常量相关的逻辑,这样您就不需要硬编码构造函数参数,而是使用_uclass_uu。新的ReflectionClassProfile::class工作正常too@mtizziani是的,但是要注意名称空间!假设你有一个名称空间City和类B,在那里B::class可以很好地工作,但是如果你在名称空间丛林中使用它们,在那里调用B::class而不使用它会导致丛林\B,即使丛林中根本没有B!两个次要的NB:首先,在5.3中,Profile可以用作reflector构造函数的参数,不需要引用简单的类名;其次,完全清楚地说,结果数组的键是字符串,而不是此处的格式可能暗示的常量。仅值得一提的是fn是。@5.3中的Benji XVI如果打开了通知,您将无法使用没有引号的配置文件,因为它将显示以下错误:注意:使用未定义的常量配置文件-假定为“配置文件”。所以我建议保留引号'Profile',最好在类内部定义与常量相关的逻辑,这样您就不需要硬编码构造函数参数,而是使用_uclass_uu。新的ReflectionClassProfile::class工作正常too@mtizziani是的,但是要注意名称空间!假设你有一个名称空间City和类B,在那里B::class可以很好地工作,但是如果你在名称空间丛林中使用它们,在那里调用B::class而不使用它会导致丛林\B,即使丛林中根本没有B!因为数组不是常量?如果你实现了一个被认为是常量的变量,那么你就有可能在无意中改变或取消设置它。换句话说,你不能依赖它们保持不变。因为数组不是常数?如果你实现了一个被认为是常量的变量,那么你就有可能在无意中改变或取消设置它。换句话说,你不能依赖它们保持不变,你可以使用。在该页面上搜索打印类常量以查看示例。使用Reflection和Cl上的ReflectionClass,可以使用函数getConstants。可以使用。在该页面上搜索打印类常量以查看示例。使用Reflection和Cl上的ReflectionClass,可以使用函数getConstants。
// [1]
$objC = new Qwerty();
var_dump($objC->getConstants_());

// [2]
var_dump(Qwerty::getConstantsStatic_());