Re: Copying an Object
| From: | Carlos Medina | Date: | Thu, 23 Sep 2010 07:49:50 +0000 |
| Subject: | Re: Copying an Object | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-308264@lists.php.net to get a copy of this message | ||
Am 23.09.2010 08:24, schrieb Peter Lind:
On 23 September 2010 02:14, Daniel Kolbo<kolb0057@umn.edu> wrote: *snip*i think the problem here is the casting posibility in PHP is not implemented yet. Like Java you can not cast objects to others. I found, dass Classes can be casted by using this code: class ParentClass {On 9/22/2010 9:11 AM, chris h wrote: Say you have two classes: human and male. Further, say male extends human. Let's say you have a human object. Then later you want to make that human object a male object. This seems to be a pretty reasonable thing to request of our objects.Perhaps if you're a C# programmer, but the PHP way of thinking is radically different. C#: This object is whatever it was currently cast to (if possible) PHP: This object is this object, whatever it was created as If you have a need to make an object switch class in PHP, then there's a 99% chance you're working against, not with the language.This type of thing would especially be easy if objects of parent classes could be cast as an object of its extended class.I'll hazard a guess and say you didn't start programming in PHP but in something else. Regards Peter Hi,
public static function cast( ParentClass $object )
{
return $object;
}
public function tellMee( $value )
{
echo $value;
}
}
class childClass extends ParentClass
{
}
$parent = ParentClass::cast( new childClass());
$parent->tellMee( 'Hallo ');
var_dump($parent);
Well yes you can get the ChildClass by using the parent class and use the methods allowed to use in the parent class (as method of parentClass in childClass). But you can not use inheritance in this construct in PHP. I think the best way to work with PHP classes is by using the Design Patterns for PHP...
Regards
Carlos