note 24326 added to function.get-class
| From: | oliver@php.net | Date: | Tue, 13 Aug 2002 12:07:53 +0000 |
| Subject: | note 24326 added to function.get-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-34928@lists.php.net to get a copy of this message | ||
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
*/
?>
--
http://www.php.net/manual/en/function.get-class.php
http://master.php.net/manage/user-notes.php?action=edit+24326
http://master.php.net/manage/user-notes.php?action=delete+24326
http://master.php.net/manage/user-notes.php?action=reject+24326