Image

re-casting objects in PHP

Hi,

Perl lets me do this:

package A;
sub new {
my ($class, @params) = @_;
my $self = { };
bless $self, ref($class) || $class;
return $self;
}

--

package B;
@ISA = qw(A);

--

my $a = new A;
bless $a, "B";
# $a is now an object of class B.

--

The equivalent in PHP to the class heirarchy is this:

class A {
function A() { };
}

class B extends A {
function B() { };
}

Now is it possible to write some code that allows "upgrading" an object of class A to be an object of class B? i.e. I want to do something equivalent to the perl "bless" function, which turns a reference into an object, however I want to do it to an object, e.g.

$a = new A();
$b = $a->transform_to_class_B();

... or similar.

Has anyone figured out a PHP 4 or 5 way of doing this?