[英]How to create constant for the values inside an array and access them outside class in php
I have created array inside a class using constant and assigned values to that array by key,value pair so I want to create const for a,const for b,const for c values, so how to create this and access those values(a,b,c) outside class using Php? 我在类中使用常量创建了数组,并通过键,值对将值分配给该数组,因此我想为a创建const,为b创建const,为c创建const,因此如何创建该常量并访问这些值( b,c)在课外使用PHP? actually I am not getting any output for this. 实际上,我对此没有任何输出。
<?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
?>
You forgot to use the correct indexes
and also to call the class
. 您忘记了使用正确的indexes
,也忘记了调用class
。 Although I don't understand why you want to create constants in this way, here comes the correct version of your current code: 尽管我不明白为什么要用这种方式创建常量,但是这里提供了当前代码的正确版本:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.