我在类中使用常量创建了数组,并通过键,值对将值分配给该数组,因此我想为a创建const,为b创建const,为c创建const,因此如何创建该数组并访问这些值(a,b,c)在课外使用Php?实际上,我对此没有任何输出。
<?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
?>
答案 0 :(得分:0)
您忘记了使用正确的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;