是否可以在带有函数的类中定义php数组常量?
edit:"php数组变量"不是常量
我试过:
假设,数组func(字符串param1,字符串param2…);
class A{
public static $const;
public function blah(){
self::$const = func('a','b','c',...);
}
}
sublime中的调试器在self::$const行的断点后没有显示$const的值
在PHP中<5.6不能将数组作为常量。
如果你想通过函数返回一个常数,你只需要做:
define('FOO', 'bar')
function getFoo() {
return FOO;
}
但这并不是返回数组,这是一个非常愚蠢的例子。
我认为您真正想要做的是从类的静态方法返回一个数组,类似于:
class Foo {
public static function GetFoo() {
return array(1, 2, 3);
}
}
Foo::GetFoo();
或者,如果您想将函数作为对象实例的方法运行,则不会设置静态属性(这对我来说没有意义)。
class Foo {
private $foo = array();
public function getFoo($arg1, $arg2) { // not sure what your arguments are for if this is intended to be a "constant"...
$this->foo = array(...)
return $this->foo
}
}
$someFoo = new Foo();
$someFoo->getFoo(1, 2);
这有帮助吗?PHP常量包含数组的众多例子?也应该有所帮助。