[nycphp-talk] Object conversion
Alexander
alex at pilgrimstudio.com
Tue Jan 31 09:19:34 EST 2006
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 :)
code is somewhat like that
class Base
{
var $a;
var $b;
var $c;
...
}
class A extends Base
{
var $d;
var $e;
function SomeKindOfTransormation(Base-type parameter)
{
//here I was trying to copy all data from argument
//as after it will be transormed and stored in the same variables
}
}
btw, as I'm using PHP4 is there any way to define function-member of the
class outside the class?
> What you've done above is make $objA a reference to $objBase - no
> "conversion".
>
> You might've meant you'd like to downcast it to a Base object. TMK,
> downcasting is not supported at all in PHP. It is commonly frowned upon
> in
> other languages because of problems that can arise. In fact Java 1.5 even
> added features 5 which resemble, or are even identical to templates in C++
> (shudder<andrew> ... ;-) to avoid having to do it in the common cases like
> collections. Anyway, I digress...
>
> If you were to do this in PHP it *might* resemble its counterparts in
> other languages:
> $objAlikeBase = (Base) $objA;
> But like I said, you can't in PHP. You may only cast between the
> primatives, not classes.
>
> If you really think you need to do this, give a better example of what
> your looking to do.
>
> Don't forget that an instance of class that extends another has the
> methods and properties of *both* classes or even interfaces. It can
> also be used as both when code requires it like:
>
> class shape { }
> interface printable{ }
> class circle extends shape implements printable { }
>
> function draw(shape $o) { }
> function myprint(printable $o) { }
>
> $dot = new circle();
>
> draw($dot);
> myprint($dot);
More information about the talk
mailing list