Is this the only way to have arrays as constants in php or is this bad code:
class MyClass
{
private static $myArray = array('test1','test2','test3');
public static function getMyArray()
{
return self::$myArray;
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your code is fine – arrays cannot be declared constant in PHP before version 5.6, so the static approach is probably the best way to go. You should consider marking this variable as constant via a comment:
With PHP 5.6.0 or newer, you can declare arrays constant: