note 24326 deleted from function.get-class by cmb

From: Date: Wed, 30 Nov 2016 16:34:04 +0000
Subject: note 24326 deleted from function.get-class by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207758@lists.php.net to get a copy of this message
Note Submitter: oliver DOT pliquett @mediagear DOT de ---- This function can become _VERY_ helpful if you want to return a new object of the same type. See this example: <?php class Foo{ var $name; function Foo( $parameter ){ $this->name = $parameter; } function whoami() { echo "I'm a " . get_class( $this ) ."\n"; } function getNew() { $className = get_class( $this ); // here it happens: return new $className ( "world" ) ; } } class Bar extends Foo { function Bar( $name ){ $this->Foo( $name ); } function welcome() { echo "Hello, " . $this->name . "! \n"; } } // We generate a Bar object: $myBar = new Bar( "Oliver" ); $myBar->welcome(); //now let's instanciate a new Bar object. //note: this method is inherited from Foo by Bar! $baba = $myBar->getNew(); $baba->welcome(); $baba->whoami(); /* Output: Hello, Oliver! Hello, world! I'm a bar */ ?>

« previous php.notes (#207758) next »