|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help me understand global $$obj;
I am trying to learn OOP (object oriented programming) in PHP.
I am working on the following example which may be found at http://aspn.activestate.com/ASPN/do...f.classobj.html I am having great difficulty understanding the purpose of the line global $$obj; as it appears in function class_parentage .... (see below) I understand that globals are variables with a greater scope, such that other functions, or even other scripts possibly, may access them. But where in the code below is $$obj initialized, unless it is initialized somehow by is_subclass_of ? When I google search for the syntax and definition of is_subclass_of it is not clear that anything is done to the first parameter. I have also looke here: http://www.zend.com/manual/language.variables.scope.php I cannot see in these code examples where $$obj ever gets initialized. I am also confused as to whether $$obj is some separate variable from $obj or whether it is somehow a syntax with an extra $ sign which elevates $obj to a global status. It is really driving me crazy, when I am trying to understand and learn something new, difficult, alien, and there is something in the midst of it all (like this $$obj) which seems to work, and yet makes no sense as to the whys and wherefores of its use. To thicken this sauce a bit.... I did a google search on class_parentage and found that this same example appears in various languages. In the FRENCH version, I find: http://www.nexen.net/docs/php/annot...bj.examples.php function class_parentage($obj, $class) { if (is_subclass_of($GLOBALS[$obj], $class)) { echo "L'objet $obj appartient à la classe ".get_class($$obj); echo " est une sous-classe de $class\n"; } else { echo "L'objet $obj n'est pas une sous-classe de $class\n"; } } (as an aside, non-sequitur, I must say that THIS url looks like something I should come back to for more study http://www.weberdev.com/search.php3...s&secondary=PHP ) Thanks for any enlightenment. ============================================ function class_parentage($obj, $class) { global $$obj; if (is_subclass_of($$obj, $class)) { echo "Object $obj belongs to class ".get_class($$obj); echo " a subclass of $class\n"; } else { echo "Object $obj does not belong to a subclass of $class\n"; } } =========================== Complete tutorial example save as classes.inc <?php // base class with member properties and methods class Vegetable { var $edible; var $color; function Vegetable( $edible, $color="green" ) { $this->edible = $edible; $this->color = $color; } function is_edible() { return $this->edible; } function what_color() { return $this->color; } } // end of class Vegetable // extends the base class class Spinach extends Vegetable { var $cooked = false; function Spinach() { $this->Vegetable( true, "green" ); } function cook_it() { $this->cooked = true; } function is_cooked() { return $this->cooked; } } // end of class Spinach ?> ============================= save as test_script.php <pre> <?php include "classes.inc"; // utility functions function print_vars($obj) { $arr = get_object_vars($obj); while (list($prop, $val) = each($arr)) echo "\t$prop = $val\n"; } function print_methods($obj) { $arr = get_class_methods(get_class($obj)); foreach ($arr as $method) echo "\tfunction $method()\n"; } function class_parentage($obj, $class) { global $$obj; if (is_subclass_of($$obj, $class)) { echo "Object $obj belongs to class ".get_class($$obj); echo " a subclass of $class\n"; } else { echo "Object $obj does not belong to a subclass of $class\n"; } } // instantiate 2 objects $veggie = new Vegetable(true,"blue"); $leafy = new Spinach(); // print out information about objects echo "veggie: CLASS ".get_class($veggie)."\n"; echo "leafy: CLASS ".get_class($leafy); echo ", PARENT ".get_parent_class($leafy)."\n"; // show veggie properties echo "\nveggie: Properties\n"; print_vars($veggie); // and leafy methods echo "\nleafy: Methods\n"; print_methods($leafy); echo "\nParentage:\n"; class_parentage("leafy", "Spinach"); class_parentage("leafy", "Vegetable"); ?> </pre> =================== OUTPUT when executed: =================== veggie: CLASS vegetable leafy: CLASS spinach, PARENT vegetable veggie: Properties edible = 1 color = blue leafy: Methods function vegetable() function is_edible() function what_color() function spinach() function cook_it() function is_cooked() Parentage: Object leafy does not belong to a subclass of Spinach Object leafy belongs to class spinach a subclass of Vegetable |
|
#2
|
|||
|
|||
|
Help came from IRC #phpfreaks
After I posted my question here, I did some google.com searching, and then decided to download mIRC irc client, and join the #phpfreaks channel.
If you go there, you must be patient and polite and persistant, (but not pesky), until someone feels like answering your question, but people there are quite knowledgable, and it is realtime response. Visit this link for detail on downloading the mIRC chat client and joining #phpfreaks http://www.phpfreaks.com/ircchan.php A very knoweldgable person there explained to me that "$$" denotes a VARIABLE VARIABLE. For example, $x = "hello"; echo $x; prints the value of $x, which is "hello" BUT when I say $$x = "Goodbye!" , what I am really doing is saying $hello = "goodbye" $$x is a synonym for a variable called $hello. So, if I write echo $$x ; it will pring "goodbye"; but should I write $x = "bananna" ; why then, $$x will be a variable called $bananna. I have not experimented with what happens to the variable $hello, possibly it still exists and can be accessed. Anyway, I am now much closer to understanding my OOP example, because $obj is passed to the function as an argument, and when you write GLOBAL $$obj; then you are referencing some variable whose name is whatever the value of $obj is. So if $obj = "leafy" or "spinach", why then $$obj will reference some variable named $leafy or $spinach. I know this seems very tricky and confusing, but with constant practice and repetition, I hope to make such things more familiar, and second nature. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Help me understand global $$obj; |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|