That is, do not create an element in one of the superglobal arrays, but create your own variable.
|
3 answers
Well, if three pieces, then this model will suit you, I think
class MyClass1 { static $instance = null; private $vars = array(); function check() { if (self::$instance == null) self::$instance = new self; }; function set($name, $val) { self::check(); self::$instance->_set($name, $val); echo "\n".$name.' = '.$val; return false; } function _set($n, $v) { $this->vars[$n] = $v; } } function myFunc() { MyClass::set('a', 1); return false; } myFunc();
- Oh thank you! I blunted it. In principle, I will manage and just a function. I forgot that the functions themselves are visible everywhere. So I can wrap the necessary things in the function) I think how beautiful it will be for me,
$GLOBALS
or through the function. A pity that superglobals cannot be. - Oleg Arkhipov - In my opinion, either in 5.3, or in 5.4, the following opportunity appeared: function myLog () {if (empty ($ GLOBALS ['myClass'])) $ GLOBALS ['myClass'] = new myClass; return $ GLOBALS ['myClass']; } myLog () -> log (); Maybe it will be interesting) In general, if it were possible to create superglobala, the number of bydlokoda would increase even more) - Sh4dow
- @ Sh4dow, what exactly appeared in 5.3 / 5.4?
return
object? - Oleg Arkhipov - @ Sh4dow about the kiss did not hear the campaign: D Why such a perversion? - Zowie
- @Construct, no, call chains a la jquery) I can lie, we can remember that in php3 / 4 I couldn’t do such a thing, although the idea was)) In any case, it gave me something like "unexpected object operator" @AlexWindHope eeem probably not heard) - Sh4dow
|
No, in PHP there is no way to create your own superglobal variables, and you don’t need it. There are many approaches in which global variables are not needed, and indeed, global variables are evil :)
You better describe the task for which you needed your superglobal variables.
- one... and solve it with a singleton. Something tells me) - Sh4dow
- Thanks for the answer, clearly. As for the task: I only needed three of them. Log objects for convenient logging from any place of the application. Now I use
$GLOBALS
, this is also normal, only more authentic. - Oleg Arkhipov - @Shadow, there, in principle, is Singleton. - Oleg Arkhipov
- . class Log {public static function read () {} public static function wright () {}} Log :: read (); Log :: wright (); It remains only to implement the logic - Zowie
- oneCreate, for example, the base class
LogBase
all other logs inherit from it. - Zowie
|
Well, if you mean a variable that will retain its value even after reloading the page, then the session will work for you. Working with them is very easy.
- No, I meant a variable that will be visible by default in all scopes. Just the type of the array
$_SESSION
, which itself is such a variable. - Oleg Arkhipov - And why $ _SESSION does not fit. Believe me, this is the best solution. - AseN
- @Deonis, unfortunately, I need an object. - Oleg Arkhipov
- Well, try $ _GLOBALS: php.ru/manual/ini.core.html#ini.register-globals - AseN
|
$GLOBALS
- Zowie