PHP OOP variable scope problem
|
12-28-2010, 04:12 PM,
(This post was last modified: 12-28-2010, 04:24 PM by RichardGv.)
|
|||
|
|||
RE: PHP OOP variable scope problem
Okay, the problem lays in a single sentence in PHP document:
Quote:When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.So, even though the line require('child.php') lays in the body of the method Init() of class Main, the class First declared in child.php actually is in global variable scope instead of Init() variable scope, and may not access any variable in the variable scope of Init() directly, including $Two. (PHP do not allow class definition in another class definition, by the way, so there's no workaround.) The $Two in child.php is actually an undefined variable and should trigger a PHP warning. And the only possible way to go back from a subclass to its instantiated parent class (well, it should be called an object) is to pass the parent object (or part of it) to the instance of subclass, i.e. change the declaration of Activate() in child.php to something like: PHP Code: function Activate($Arg, $obj) { PHP Code: $this->One->Activate("the Oven", $this); It's not a fault of those lovely PHP developers : PHP is not so smart that it can know which instance of parent class you exactly want to return to, when there's more than one instance. Also, there's another fatal issue in your script: You are incorrectly using static attributes/methods of classes, all the time. Quote:Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).So, if you explicitly conform to PHP standard, within the method Init() of class Main, you may not access $this->One or $this->Two. But currently PHP is only printing a few warning messages in this situation (which you may have oppressed through PHP configuration). But still, PHP tolerates it does not mean you should use it. It generates very puzzling code. Another problem is the method A() of the class Second is not a static method, and should not be access by Second::A() (:: is for static methods/attributes) in child.php. PHP prints a warning for this, too, but you or your PHP environment ignored it.
Gentoo Linux User (w/ fvwm) / Loyal Firefox User / Owner of a Stupid Old Computer - My PGP Public Key
No man is an island, entire of itself; every man is a piece of the continent, a part of the main; if a clod be washed away by the sea, Europe is the less, as well as if a promontory were, as well as if a manor of thy friends or of thine own were; any man's death diminishes me, because I am involved in mankind; and therefore never send to know for whom the bell tolls; it tolls for thee. -- Devotions Upon Emergent Occasions (1624), John Donn |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
PHP OOP variable scope problem - by Vanilla - 12-28-2010, 08:10 AM
RE: PHP OOP variable scope problem - by RichardGv - 12-28-2010, 04:12 PM
RE: PHP OOP variable scope problem - by Vanilla - 12-29-2010, 12:59 AM
RE: PHP OOP variable scope problem - by RichardGv - 12-29-2010, 11:26 AM
RE: PHP OOP variable scope problem - by Vanilla - 12-30-2010, 02:20 AM
RE: PHP OOP variable scope problem - by Vanilla - 01-03-2011, 09:09 AM
|