This article mainly introduces how to define an array constant (array constant) in PHP. This article uses the const keyword and eval () function in the class to implement it, if you need it, you can refer to PHP. we cannot use const to directly define array constants, but const can define string constants and use the eval () function to execute string constants. Therefore, we can return an array constant by defining a string constant. Here is the time to witness the miracle!
The code is as follows:
<? Php
Class Test
{
Const MY_ARR = "return array (\" a \ ", \" B \ ", \" c \ ", \" d \");";
Public function getConstArray ()
{
Return eval (Test: MY_ARR); // The eval () function executes the string as the PHP code.
}
}
$ T = new Test ();
Print_r ($ t-> getConstArray ());
?>
In the above code, the getConstArray () function is equivalent to an array constant.