-2

I need to store an array to a constant. I am using PHP 5.6, so cant use define. I am getting that data from database.

It is working for -

const VAR_DATA = array(1, 2, 3);

But when I do -

$var = array(1, 2, 3);
const VAR_DATA = $var;

It is giving me syntax error.

How can I achieve that?

Not duplicate as That question dose not answer how to achieve this. For regular arrays it is working but when I am using a variable it is giving me the error.

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87

5 Answers5

2

You can save array like this

define('VAR_DATA', implode(',',array(1, 2, 3)));

And wherever you want to use it, just explode it by

$array = explode(',',VAR_DATA);
GYaN
  • 2,327
  • 4
  • 19
  • 39
1

The value must be a constant expression, not (for example) a variable, a property, or a function call.see http://php.net/manual/en/language.oop5.constants.php

and you can define a constant with const like this ,const SOMEVAR = [1,2,4]

1

from Document

In PHP 5, value must be a scalar value (integer, float, string, boolean, or NULL)

Objects and Arrays cannot be stored in constants without serializing them first

AZinkey
  • 5,209
  • 5
  • 28
  • 46
1

Assuming your const is declared in a class (called VarClass below), you could have public static access instead of a const:

class VarClass
{
    public static function VAR_DATA()
    {
        return $your_data_here;
    }
}

or even

class VarClass 
{
    public static $VAR_DATA;
}

and set it once you retrieved it from the database:

VarClass::$VAR_DATA = $your_data;

and then get it:

$get_data = VarClass::$VAR_DATA;

I am getting that data from database.

If you get the data array from the database, then why do you consider it to be a const? Apparently, it comes into existence at some point in your code execution. From there on you want to treat it as immutabel data.

The solution above solves the access problem, but does not solve the immutability. Using a Singleton could help with that, but might be overkill in your scenario.

Stefan
  • 1,325
  • 12
  • 25
  • That array will be used multiple times, in multiple classes, in multiple functions. I want to avoid that changes in function calls and all. If I could define the constant in middleware or something that constant would be present and accessed without changing function calls – Sougata Bose Oct 09 '17 at 07:38
  • *The solution above solves the access problem, but does not solve the immutability. Using a Singleton could help with that, but might be overkill in your scenario.* Yup.. – Sougata Bose Oct 09 '17 at 07:39
  • So you basically have legacy code that uses the VAR_DATA const, but it actually is no longer a const, as meanwhile the data is to be fetched from the database. However, you don't want to chenge the legacy code? – Stefan Oct 09 '17 at 07:40
  • Yup. Kind of that. Now I am going to use `serialize` for this. – Sougata Bose Oct 09 '17 at 09:57
0

you can do one trick save your data in .env like this

.env file

VARIABLE_NAME=Value1,Value2,Value3

config/app.php

return [
  'VARIABLE_NAME' => explode(',', env('VARIABLE_NAME'))
];

hope it will solve your prob

Gaurav Gupta
  • 1,588
  • 2
  • 14
  • 21