note 46881 added to language.oop5.abstract

From: Date: Tue, 26 Oct 2004 21:39:03 +0000
Subject: note 46881 added to language.oop5.abstract
Groups: php.notes 
Request: Send a blank email to php-notes+get-79143@lists.php.net to get a copy of this message
A couple notes: The following will now work (even if ObjectName is a sibling or child) $this = new ObjectName; //error parent::parent::function(); //interesting error This demo shows how to create a generic abstraction one class can act similarly to other classes, and change at runtime as needed. if a parent has function() defined, but the child does not, the following are identical: parent::function(); //always use parents self::function(); //use own if possible, parents otherwise $this->function(); //same as self Try the following: <? abstract class abby { //used when no decendant whoami is defined public function whoami() { echo "I'm a child of Abby!<BR>\n"; } //abstract --> no body/definition abstract public function greet(); } //Child of abby class typea extends abby { //it has it's own greet, mom handles whoami public function greet() { echo "<B>Hello World!</B> I'm Type 1<BR>\n"; parent::whoami(); } } class typeb extends abby { //mom still handles whoami, but implicitly public function greet() { echo "<B>Hello World!</B> I'm Type 2<BR>\n"; self::whoami(); } } //grandchild of abby class typec extends typeb { public function greet() { echo "<B>Hello World!</B> I'm Type 3<BR>\nMy parent says:<BR><HR>\n"; parent::greet(); echo "<HR>When I ask my parent 'who are you?', it says:<BR><HR>\n"; parent::whoami(); echo "<HR>I am:<BR>\n"; //implicitly calls parent's whoami $this->whoami(); } } class genny extends abby { private $internal = 0; public function whoami() { if($this->internal == 0) echo "I'm Genni, the generic wrapper needed because you can't assign \$this to something new, like a child or sibling<BR>\n"; else $this->internal->whoami(); } public function greet() { if($this->internal == 0) { echo "<B>Hello World!</B> I'm Genny, the initial class for a generic Abby object!<BR>\n"; self::whoami(); //same as $this->whoami() } else $this->internal->greet(); } //new functions public function morph($type) { switch($type) { case 1: $this->internal = new typea; break; case 2: $this->internal = new typeb; break; case 3: $this->internal = new typec; break; default: echo "I can't change into a type $type... Going to my normal form!<BR>\n"; $this->internal = 0; } echo "<BR><BR>\n"; } } $var = new genny; echo "Genny is here!<BR>\n"; $var->greet(); echo "Uh-oh, something weird is happening to Genny!<BR>\n"; $var->morph(1); $var->greet(); echo "Umm, it's happening again?<BR>\n"; $var->morph(2); $var->greet(); echo "Oh my, this Genny, she's... I mean Type A, err, Type B now... Weird...<BR>\n"; $var->morph(3); $var->greet(); echo "Dear me, it's trying again, something doens't look right though...<BR>\n"; $var->morph(4); $var->greet(); ?> ---- Manual Page -- http://www.php.net/manual/en/language.oop5.abstract.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46881 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46881&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46881&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46881&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46881&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46881&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46881&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46881&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#79143) next »