类方法PHP内部的常量作用域


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;
}

}

https://www.php.net/manual/en/language.oop5.constants.php

最新更新