Re: [PHP] Class Auto-Assigning to Variable
On Wed, 2013-08-07 at 19:02 -0600, Brian Smither wrote: > >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. No, you just didn't post relevant code. > > > >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. > $GLOBALS['hello'] is essentially a synonym for $hello in this context. $GLOBALS is just an array of all variables that have global scope, so adding to this causes variables to be added at the global scope level. But, like I've said a few times, this might not be your specific problem, and without seeing the actual code that causes this problem, it's very difficult for anyone to help. Your code examples don't exhibit the issue you describe, so they're not useful in the context of pin-pointing the problem. If you can post code that *does* give that problem, then someone on the list may be able to help. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Class Auto-Assigning to Variable
>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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Auto-Assigning to Variable
>I have two dozen classes in this application. In every case, there will >be a variable, the name of which is a lowercase variant of the class >name, to which is assigned an instance of the class, when the class's >construct() function completes. The example informs you of this. > Actually, as we've explained, 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 >> I would say for definite that it's some of the surrounding code, > >Exactly. No sooner and no later than precisely when the class's >construct() function ends, and control is given to the next statement >after the one that instantiated that class. > Is your class maybe inheriting from another one and that contains the code causing this issue? > >> probably something similar to this: > >Let's explore this statement: > >> $GLOBALS[strtolower(get_class($this))] = $this; > >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. > >This: >http://php.net/manual/en/reserved.variables.globals.php >implies the opposite direction. > Not sure what you mean, but this super global works both ways. Don't necessarily get hung up on this being the exact code, it's just a proof of concept of how it /might/ be happening. Like I said, there are probably other ways too. > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php Thanks, Ash -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Auto-Assigning to Variable
> $hello = $clsHello; If that conceptual statement (or any occurance of the conceptual $hello) were in the code, then my (really good) Find feature of my code editor would have found it. > There are only a few variables that get assigned as side effects of > functions, but they have very specific names, and none of them are > $hello (but I'm guessing that's not the actual variable name) I have two dozen classes in this application. In every case, there will be a variable, the name of which is a lowercase variant of the class name, to which is assigned an instance of the class, when the class's construct() function completes. The example informs you of this. > Somewhere in your code there is something that is assigning to $hello. > Find everything that's doing that and look at each instance in detail. I have. Many times. What I am looking for is a side-effect -- something not obvious. (This application does not use an eval() function where some obfuscated string manipulation would play this out.) > I would say for definite that it's some of the surrounding code, Exactly. No sooner and no later than precisely when the class's construct() function ends, and control is given to the next statement after the one that instantiated that class. > probably something similar to this: Let's explore this statement: > $GLOBALS[strtolower(get_class($this))] = $this; May I infer that the declaration of $GLOBALS['hello'] will, at the same time, also create $hello (without a statement declaring such)? This: http://php.net/manual/en/reserved.variables.globals.php implies the opposite direction. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Auto-Assigning to Variable
On Wed, 2013-08-07 at 21:02 +0100, Stuart Dallas wrote: > On 7 Aug 2013, at 20:45, "Brian Smither" wrote: > > >> I cannot replicate this. > > > > I don't expect anyone to be able to replicate this behavior. The example > > shows an extraordinarily stripped-down sequence of statements that informs > > what should work, but do to some unknown agent, which, therefore, cannot be > > included in the example, produces unexpected output. > > > > This conceptual example is not a 'sample' or 'excerpt' of the actual > > application. There is much, much more code. None of it is an apparent > > likely suspect in causing this behavior. > > > > I am hoping for a, "Oh, yeah! I've seen that happen before. It's caused > > by..." > > > > Let us focus on the central question: > > > > Would there be a PHP function that would do [what the example describes] as > > a side-effect? > > Yes: > > $hello = $clsHello; > > There are only a few variables that get assigned as side effects of > functions, but they have very specific names, and none of them are $hello > (but I'm guessing that's not the actual variable name) and none of them will > assign a userland object. Somewhere in your code there is something that is > assigning to $hello. Find everything that's doing that and look at each > instance in detail. > > -Stuart > > -- > Stuart Dallas > 3ft9 Ltd > http://3ft9.com/ > As you've said that your excerpt doesn't replicate the problem, it's not that useful really to accurately illustrate it. I would say for definite that it's some of the surrounding code, probably something similar to this: class Hello { private $_world = 'World'; function __construct(){} function test() { $GLOBALS[strtolower(get_class($this))] = $this; } } $clsHello = new Hello(); $clsHello->test(); echo 'The variable $hello is '.gettype($hello)."\n".print_r($hello,true); There are probably many ways to achieve this, I can't think of a sane reason for any of them though! Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Class Auto-Assigning to Variable
On 7 Aug 2013, at 20:45, "Brian Smither" wrote: >> I cannot replicate this. > > I don't expect anyone to be able to replicate this behavior. The example > shows an extraordinarily stripped-down sequence of statements that informs > what should work, but do to some unknown agent, which, therefore, cannot be > included in the example, produces unexpected output. > > This conceptual example is not a 'sample' or 'excerpt' of the actual > application. There is much, much more code. None of it is an apparent likely > suspect in causing this behavior. > > I am hoping for a, "Oh, yeah! I've seen that happen before. It's caused by..." > > Let us focus on the central question: > > Would there be a PHP function that would do [what the example describes] as a > side-effect? Yes: $hello = $clsHello; There are only a few variables that get assigned as side effects of functions, but they have very specific names, and none of them are $hello (but I'm guessing that's not the actual variable name) and none of them will assign a userland object. Somewhere in your code there is something that is assigning to $hello. Find everything that's doing that and look at each instance in detail. -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Auto-Assigning to Variable
>I cannot replicate this. I don't expect anyone to be able to replicate this behavior. The example shows an extraordinarily stripped-down sequence of statements that informs what should work, but do to some unknown agent, which, therefore, cannot be included in the example, produces unexpected output. This conceptual example is not a 'sample' or 'excerpt' of the actual application. There is much, much more code. None of it is an apparent likely suspect in causing this behavior. I am hoping for a, "Oh, yeah! I've seen that happen before. It's caused by..." Let us focus on the central question: Would there be a PHP function that would do [what the example describes] as a side-effect? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Auto-Assigning to Variable
On Wed, 2013-08-07 at 13:11 -0600, Brian Smither wrote: > Second go around: > > I have a situation where, for some unknown reason, where each class that > finishes its __contruct{} function, that class gets automatically assigned to > a variable - other than the variable I specify. > > Conceptually (a little bit better on the conceptualizing): > > class Hello { > private $_world = 'World'; > function __construct(){} > } > > $clsHello = new Hello(); > > echo 'The variable $hello is '.gettype($hello)."\n".print_r($hello,true); > > Output: > The variable $hello is object > Hello Object > ( > [_world:Hello:private] => World > ) > > There is no statement in my application that assigns an instance of the class > to another variable, the name being a lowercase variant of the class name. > > Would there be a PHP function that would do this as a side-effect? > > I am more interested in learning what is happening as opposed to rolling back > to a previous version. (A backup copy functions fine. A file compare does not > reveal any likely suspects.) > > PHP5.4.17-NTS-VC9 (Windows XP-SP3) > > > > I cannot replicate this. That code you supplied correctly gives a notice warning about an undefined variable '$hello'. Are you sure that that simple code excerpt is giving you the problem you describe and there's not more to the whole thing, more code, etc? Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Class Auto-Assigning to Variable
Second go around: I have a situation where, for some unknown reason, where each class that finishes its __contruct{} function, that class gets automatically assigned to a variable - other than the variable I specify. Conceptually (a little bit better on the conceptualizing): class Hello { private $_world = 'World'; function __construct(){} } $clsHello = new Hello(); echo 'The variable $hello is '.gettype($hello)."\n".print_r($hello,true); Output: The variable $hello is object Hello Object ( [_world:Hello:private] => World ) There is no statement in my application that assigns an instance of the class to another variable, the name being a lowercase variant of the class name. Would there be a PHP function that would do this as a side-effect? I am more interested in learning what is happening as opposed to rolling back to a previous version. (A backup copy functions fine. A file compare does not reveal any likely suspects.) PHP5.4.17-NTS-VC9 (Windows XP-SP3) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class Auto-Assigning to Variable
2013/8/7 Brian Smither > I have a situation where, for some unknown reason, where each class that > finishes its __contruct{} function, that class gets automatically assigned > to a variable - other than the variable I specify. > > Conceptually: > > class Hello { private $_world = 'World'; __construct(){} } > This isn't even valid PHP $ php -a Interactive shell php > class Hello { private $_world = 'World'; __construct(){} } PHP Parse error: syntax error, unexpected '__construct' (T_STRING), expecting function (T_FUNCTION) in php shell code on line 1 php > > $clsHello = new Hello(); > > echo 'The variable $hello is '.gettype($hello)."\n".print_r($hello,true); > > Output: > The variable $hello is object > Hello Object > ( > [_world:Hello:private] => World > ) > > There is no statement in my application that assigns an instance of the > class to another variable, the name being a lowercase variant of the class > name. > > Would there be a PHP function that would do this as a side-effect? > > I am more interested in learning what is happening as opposed to rolling > back to a previous version. (A backup copy functions fine. A file compare > does not reveal any likely suspects.) > > PHP5.4.17-NTS-VC9 (Windows XP-SP3) > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- github.com/KingCrunch
[PHP] Class Auto-Assigning to Variable
I have a situation where, for some unknown reason, where each class that finishes its __contruct{} function, that class gets automatically assigned to a variable - other than the variable I specify. Conceptually: class Hello { private $_world = 'World'; __construct(){} } $clsHello = new Hello(); echo 'The variable $hello is '.gettype($hello)."\n".print_r($hello,true); Output: The variable $hello is object Hello Object ( [_world:Hello:private] => World ) There is no statement in my application that assigns an instance of the class to another variable, the name being a lowercase variant of the class name. Would there be a PHP function that would do this as a side-effect? I am more interested in learning what is happening as opposed to rolling back to a previous version. (A backup copy functions fine. A file compare does not reveal any likely suspects.) PHP5.4.17-NTS-VC9 (Windows XP-SP3) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php