PHP OOP variable scope problem
|
12-28-2010, 08:10 AM,
|
|||
|
|||
PHP OOP variable scope problem
Aight', I've never dug deep into the php OOP thing, as I've always relied on procedural programming or OOP on a very low level...bla...bla...
Silly me, ANYWAY, I've run into a problem, I just can't solve. I donno' whether I should use $this, parent, self, :: (Scope resolution operator). Or whether the static keywords is even supposed to be there. The case is, that I have a main class, in which I've instantialized(?) two secondary classes (Main -> One and Main -> Two). Afterwards, through the main class I've changed instance Two's variable $Xenon to be " in ". But I keep getting an: Code: Trying to get property of non-object Don't mind the resulting sentence, it's just random stuff Main.php PHP Code: class Main child.php PHP Code: class First extends Main child2.php PHP Code: class Second extends Main I've found out, that I can get the result I want if I pinpoint the $Xenon variable from an absolute/global perspective, like this: PHP Code: function Activate($Arg) Regards and thanks in advance - Vanilla |
|||
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 |
|||
12-29-2010, 12:59 AM,
|
|||
|
|||
RE: PHP OOP variable scope problem
Thanks Richard! You're a hero! I give you +1 Positive for that one :)
This was what I was looking for: PHP Code: function Activate($Arg, $obj) { PHP Code: $this->One->Activate("the Oven", $this); I don't know why I forgot that you could pass on an instance as an argument, as I used to do it in AS 3 all the time. Regarding the static thing, I don't know why PHP doesn't print error messages. I've set php to display all errors in the .ini file. It's probably a windows fault. Anyway, I was just wondering. If I had a lot of properties/vars, would it be feasible to load them onto an array, and then pass the array as an argument (To Activate())? Would that be a valid way to do such without generating errors? |
|||
12-29-2010, 11:26 AM,
(This post was last modified: 12-29-2010, 11:31 AM by RichardGv.)
|
|||
|
|||
RE: PHP OOP variable scope problem
(12-29-2010, 12:59 AM)Vanilla Wrote: Regarding the static thing, I don't know why PHP doesn't print error messages. I've set php to display all errors in the .ini file. Hmm, no, it isn't a Windows fault, I just found out. E_STRICT Strict Standard warnings are not enabled by default in PHP. I have it enabled since XAMPP does by default. To enable E_STRICT warnings, change error_reporting in php.ini to: Code: error_reporting = E_ALL | E_STRICT (12-29-2010, 12:59 AM)Vanilla Wrote: Anyway, I was just wondering. If I had a lot of properties/vars, would it be feasible to load them onto an array, and then pass the array as an argument (To Activate())? Would that be a valid way to do such without generating errors?
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 |
|||
12-30-2010, 02:20 AM,
|
|||
|
|||
RE: PHP OOP variable scope problem
Aight' - that's just what I wanted to know, thanks for the elaboration ;)
|
|||
01-03-2011, 09:09 AM,
|
|||
|
|||
RE: PHP OOP variable scope problem
[Problem solved - Thread closed]
|
|||
« Next Oldest | Next Newest »
|