Php const arrays

This is the only way to have arrays as constants in php or this is bad code:

class MyClass { private static $myArray = array('test1','test2','test3'); public static function getMyArray() { return self::$myArray; } } 
+47
php
Aug 26 2018-12-12T00:
source share
7 answers

Your code is fine: arrays cannot be declared persistent in PHP prior to version 5.6, so a static approach is probably the best way. You should consider marking this variable as a constant through a comment:

 /** @const */ private static $myArray = array(...); 

With PHP 5.6.0 or later, you can declare arrays as constant:

 const myArray = array(...); 
+59
Aug 26 2018-12-12T00:
source share

Starting with PHP 5.6.0 (August 28, 2014), you can define an array constant (see PHP 5.6.0 new functions ).

 class MyClass { const MYARRAY = array('test1','test2','test3'); public static function getMyArray() { /* use `self` to access class constants from inside the class definition. */ return self::MYARRAY; } } /* use the class name to access class constants from outside the class definition. */ echo MyClass::MYARRAY[0]; // echo 'test1' echo MyClass::getMyArray()[1]; // echo 'test2' $my = new MyClass(); echo $my->getMyArray()[2]; // echo 'test3' 

As of PHP 7.0.0 (03 Dec 2015), array constants can be defined using define (). In PHP 5.6, they can only be defined using const. (See What's New in PHP 7.0.0 )

 define('MYARRAY', array('test1','test2','test3')); 
+12
May 4 '15 at
source share

I came across this topic, looking for the answer myself. After thinking, I would have to pass my array through every function in which it was needed. My experience with arrays and mysql made me wonder if serialization would work. Of course it is.

 define("MYARRAY", serialize($myarray)); function something() { $myarray= unserialize(MYARRAY); } 
+8
Oct 24 '13 at 7:38
source share

Static marking is a good alternative. Here's an example of encapsulating a static array to get a constant behavior.

 class ArrayConstantExample { private static $consts = array( 'CONST_MY_ARRAY' => array( 1,2,3,4 ) ); public static function constant($name) { return self::$consts[$name]; } } var_dump( ArrayConstantExample::constant('CONST_MY_ARRAY') ); 

Print

 array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) } 
+1
Oct 10 '14 at 20:00
source share

I suggest using the following:

 class MyClass { public static function getMyArray() { return array('test1','test2','test3'); } } 

Thus, you have an array of constants, and you are guaranteed that no one can change it, even the method in the class itself.

Possible micro-optimization (not sure how optimized PHP compilers are currently):

 class MyClass { public static function getMyArray() { static $myArray = array('test1','test2','test3'); return $myArray; } } 
+1
Feb 15 2018-01-15 at
source share

You created a static array, not a constant array. Static variables are mutable; constants are immutable. Your code is not bad, but it does not do what you intend to do.

In PHP 5.6 you can declare const arrays. See my previous explanations .

Maybe you want something like this:

 class MyClass { const MY_ARRAY = array('test1','test2','test3'); public function getMyArray() { return MY_ARRAY; } } 

Note that constants do not have the $ prefix, which indicates their immutability. $foo is a variable; FOO no. In addition, persistent names are always capitalized, at least in the programming languages ​​I have been exposed to. This is not performed by the compiler; it's just a (almost?) universal coding style. The visibility keywords public , protected and private do not apply to constants. Finally, static can be applied or can be applied depending on whether you want the function to be static .

+1
May 7 '15 at 10:56
source share

Starting with PHP 5.6, you can define a constant as a scalar expression, as well as define an array constant.

 class foo { const KEYS = [1, 3, 6, 7]; } // echo foo::KEYS[0]; // 1 
+1
Jan 6 '16 at 11:01
source share



All Articles