[nycphp-talk] Object conversion
Dan Cech
dcech at phpwerx.net
Tue Jan 31 09:33:55 EST 2006
Alexander wrote:
> Thank you for detailed explanation...basically I was trying to do the
> following:
>
> Object of the base class($objBase) containing some data which I want to
> copy to object of the derived class($objA). So I was looking for different
> approches to this 'problem', and I was questioning myself if it is
> possible to do that with type-casting, which you said is not supported in
> PHP :) I guess it's good :)
In PHP4 you could use a variant of the factory pattern, something like:
class Base
{
var $a;
}
class A
{
var $a;
var $b;
function &frombase(&$base)
{
$a =& new A();
$a->a =& $base->a;
return $a;
}
}
$obj =& new Base();
$obj = a::frombase($obj);
Note the use of the assign-by-reference operator, which should ensure
that any internal and external references are kept intact (I think).
Dan
More information about the talk
mailing list