Re: Copying an Object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am 23.09.2010 08:24, schrieb Peter Lind:
On 23 September 2010 02:14, Daniel Kolbo<kolb0057@xxxxxxx>  wrote:

*snip*

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,
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
{
    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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux