note 21241 deleted from language.oop.constructor by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 18:58:27 +0000 |
| Subject: | note 21241 deleted from language.oop.constructor by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162542@lists.php.net to get a copy of this message | ||
Note Submitter: eric+nospam at ypass dot net
----
This works in PHP 4.1.2 and probably versions > 4.x.
Creating an object from a constructor of a different class type can be accomplished.
Note that you can NOT return an object from a constructor. A constructor can never return a value.
However, you can set the value of $this to acheive this.
For example:
<?
class A
{
function A()
{
$this = new B();
}
}
class B
{
function iamab()
{
echo "i am an object of class B";
}
}
$a = new A();
echo '$a is a ' . get_class($a) . "\n";
$a->iamab()
?>
would print out:
$a is a b
i am an object of class B