Re: Class Auto-Assigning to Variable

From: Date: Thu, 08 Aug 2013 01:02:07 +0000
Subject: Re: Class Auto-Assigning to Variable
References: 1 2 3 4 5 6 7 8  Groups: php.general 
Request: Send a blank email to php-general+get-321814@lists.php.net to get a copy of this message
>Your example does _not_ show this, it works >as expected and throws a notice, from which can be inferred there is other >code doing this Or relevant code having a side-effect not currently realized. >Is your class maybe inheriting from another one and that contains the code >causing this issue? No. >>May I infer that the declaration of $GLOBALS['hello'] will, at the same >>time, also create $hello (without a statement declaring such)? > >The $GLOBALS array is what's known as a super global. Elements within the >array are reflected as global variables and vice-versa. Let's refine the conceptual example: class Hello { private $_world = 'World'; function __construct(){} } $GLOBALS['hello'] = new Hello(); echo 'The variable $hello is '.gettype($hello)."\n".print_r($hello,true); Can we then postulate that the value of $GLOBALS['hello'] will also be revealed in $hello, even though $hello was never formally declared? I know that $GLOBALS['hello'] has a universal scope, and $hello (wherever it comes from) needs to be global-ized inside functions.

« previous php.general (#321814) next »