Re: note 8487 added to Classes and Objects

From: Date: Thu, 14 Sep 2000 22:53:18 +0000
Subject: Re: note 8487 added to Classes and Objects
Groups: php.notes 
Request: Send a blank email to php-notes+get-4970@lists.php.net to get a copy of this message
Simon, I am not sure why you need this particular type of casting with the very simple imnplementation of classes and objects in PHP. In the example you give, you want a parent object (B) to behave like if it was a children, perhaps because you need some methods from the children, if so, why not create a children object (A) in the first place. The "content" bit is not clear, if A is inheriting from B, then it has all the vars and methods of it, or you are refering at the "contents" of the instance $b in particular? In any case, I am not sure that class casting (as in Java for example) is in PHP, but you can kludge it for very simple cases. Look at the code below and you'll see how it could be done: <?php class foo { function foo($name="foo") { $this->name=$name; } } class bar extends foo { function boom() { echo "BOOOM!"; } } $f = new foo(); $temp = explode(":",serialize($f)); $temp[2] = "\"bar\""; $c = unserialize(implode(":",$temp)); $c->boom(); ?> Will put this code in your note as an "Editor's note" Cheers --- simon.li@hongkong.com wrote: > About casting of object, say, i got: > > class A extends B { > function A() { > $this->B(); > } > .... > } > > $b = new B(); > $a = new A(); > > I wanna have a object of class A with "content" of > $b: > $a = (A) $b; // not a valid code,(right?) as for > illustration. > > http://www.php.net/manual/language.oop.php > > > -- > PHP Notes Mailing List (http://www.php.net/) > To unsubscribe, e-mail: > php-notes-unsubscribe@lists.php.net > For additional commands, e-mail: > php-notes-help@lists.php.net > To contact the list administrators, e-mail: > php-list-admin@lists.php.net > ===== --- Jesus M. Castagnetto (jcastagnetto@yahoo.com) __________________________________________________ Do You Yahoo!? Yahoo! Mail - Free email you can access from anywhere! http://mail.yahoo.com/

« previous php.notes (#4970) next »