Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disallow variable overwrite in PHP

Is it possible to prevent variables from being overwritten in PHP? I am making a system that has some reserved variables and I don't want them to be replaced with something else after a certain point. It is possible? If not, what can I do to approach something close to this?

Some of these vars are instantiated classes so I can't define them as constants.

like image 491
Vinicius Tavares Avatar asked Dec 06 '22 13:12

Vinicius Tavares


2 Answers

Take a look at this question. Also the information about constants in the PHP manual may be helpful.

like image 127
n8schloss Avatar answered Dec 21 '22 12:12

n8schloss


Yes, they're called constants.

If you cannot use them, assuming you're running the latest PHP version, you can use namespaces, using namespaces, you can have 2 variables of the same name, on different namespaces. So that you don't have collisions.

like image 40
Madara's Ghost Avatar answered Dec 21 '22 13:12

Madara's Ghost