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. For more information, see
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. For more information, see
In 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.
,