类内常量作用域方法PHP
要访问类类内常量作用域方法PHP,php,class,methods,scope,constants,Php,Class,Methods,Scope,Constants,要访问类a中的常量a,需要使用self::a。如果要在类A之外访问公共常量,可以使用A::A。为了使你的常数是全局的,你需要在任何类之外定义它。我投票结束这个问题,因为答案是正面和中间的 class A { public const a = "Constant"; public function getConstant() { echo a; //why is this undefined the 'const a' scope s
a
中的常量a
,需要使用self::a
。如果要在类A
之外访问公共常量,可以使用A::A
。为了使你的常数是全局的,你需要在任何类之外定义它。我投票结束这个问题,因为答案是正面和中间的
class A
{
public const a = "Constant";
public function getConstant()
{
echo a; //why is this undefined the 'const a' scope should be available for this block too like get function
}
}
const b = "Constant";
function get()
{
echo a;//'const b' scope is available for this block
}
get();
$obj = new A();
$obj->getConstant(); //Fatal error: Uncaught Error: Undefined constant "a"
class A {
public const a = "Constant";
public function getConstant()
{
echo self::a;
}
}