I want to make a global variable that can be used inside functions. But a few hours of experiments did not give me any results. What I want:
$qwe='knot'; function(){ echo $qwe; } At the same time, I do not want to use the global operator or access the variable through the superglobal array $GLOBALS , because I have a lot of functions in the code and each of them requires global variables, and it’s just not aesthetically pleasing and causes me to get riddled.
I tried to create a constant with ArrayObject inside to minimize the code and get the following syntax inside functions:
define('q',new ArrayObject($_POST)); function(){ echo q->qwe; } Although the constant is available inside functions, you can only put a regular array in it, and, moreover, you cannot assign new properties to it. Therefore, it is impossible to work with this option.
I also saw the option to install an additional php extension to create my own superglobal variables in php.ini . Seen here . It would be a great option if you could do the same thing, but right in the code.
Is there any way today, even the most obscene, to create a global variable in PHP?
PS A static field in a class in no way resembles a variable and does not fit the question.
PSS Please do not need to convince me to rebuild the program architecture, I need an answer to this specific question, at least for cognitive reasons.
I can not help those who do not understand the question. How can I fix my post so that these people become smarter? I need a global variable - I am offered to write singltons or functions and wonder why I do not accept their answers. BUT I ABOUT CREATING A VARIABLE ASKED! My question does not sound like: "one hundred and one way to access the global variable from a function", but apparently for all it is exactly that. Actually, my question is very interesting and the solution that is really savvy is not enough for the whole PHP community.
And apparently any attempt at reasoning and discussion about the question asked leads to confusion and misunderstanding. Next time, I’ll just be minus and roughly otshivat everyone who offers off-topic options.
Мне нужен способ создать глобальную переменную- so use$GLOBALS. This is the best option, but you want crutches, well ... - Manitikyl