我已经使用常量在类中创建了数组,并通过键、值对将值赋给该数组,所以我想为a创建常量,为b创建常量,为c值创建常量,那么如何使用Php在类外创建常量并访问这些值( a,b,c)?实际上,我没有得到任何输出。
<?php
class foo {
const arrayOfvalues = [
'a' => 'text for a',
'b' => 'text for b',
'c' => 'text for c'
];
const for_avalue= foo::arrayOfvalues[0]; //create constant for a
const for_bvalue= foo::arrayOfvalues[0]; //create constant for b
const for_cvalue= foo::arrayOfvalues[0]; //create constant for c
}
echo 'current const value'. for_avalue; //call a value by its constant name
?>发布于 2018-11-02 17:47:21
您忘记了使用正确的indexes,也忘记了调用class。虽然我不明白为什么要用这种方式创建常量,但下面是当前代码的正确版本:
class foo {
const arrayOfvalues = [
'a' => 'text for a',
'b' => 'text for b',
'c' => 'text for c'
];
const for_avalue= foo::arrayOfvalues['a']; //create constant for a
const for_bvalue= foo::arrayOfvalues['b']; //create constant for b
const for_cvalue= foo::arrayOfvalues['c']; //create constant for c
}
echo 'current const value'. foo::for_avalue;https://stackoverflow.com/questions/53116078
复制相似问题