note 92379 added to language.types.object
| From: | spidgorny at gmail dot com | Date: | Wed, 22 Jul 2009 08:58:34 +0000 |
| Subject: | note 92379 added to language.types.object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158569@lists.php.net to get a copy of this message | ||
If having
class BugDetails extends Bug
you want to cast an object of a Bug to BugDetails like this
<?php
$clone = (BugDetails) clone $this;
// OR
$clone = (BugDetails) $bug;
?>
which doesn't work in PHP, you have two options:
1. Copying all (including private) properties manually (you could also use get_object_vars(), but
this is shorter):
<?php
$clone = new BugDetails();
foreach ($this as $key => $val) {
$clone->$key = $val;
}
?>
2. Serialize an object Bug, manipulate the resulting string so that it has BugDetails inside and
unserialize it.
See here: http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/
Spent two hours looking for more elegant solution, that's my findings.
----
Server IP: 217.13.201.10
Probable Submitter: 84.233.202.2
----
Manual Page -- http://www.php.net/manual/en/language.types.object.php
Edit -- https://master.php.net/note/edit/92379
Del: integrated -- https://master.php.net/note/delete/92379/integrated
Del: useless -- https://master.php.net/note/delete/92379/useless
Del: bad code -- https://master.php.net/note/delete/92379/bad+code
Del: spam -- https://master.php.net/note/delete/92379/spam
Del: non-english -- https://master.php.net/note/delete/92379/non-english
Del: in docs -- https://master.php.net/note/delete/92379/in+docs
Del: other reasons-- https://master.php.net/note/delete/92379
Reject -- https://master.php.net/note/reject/92379
Search -- https://master.php.net/manage/user-notes.php