Cast the current object ($this) to a descendent class

Casting to change the object’s type is not possible in PHP (without using a nasty extension). Once you instantiate a object, you can’t change the class (or other implementation details) anymore…

You can simulate it with a method like so:

public function castAs($newClass) {
    $obj = new $newClass;
    foreach (get_object_vars($this) as $key => $name) {
        $obj->$key = $name;
    }
    return $obj;
}

Usage:

$obj = new MyClass();
$obj->foo = 'bar';
$newObj = $obj->castAs('myChildClass');
echo $newObj->foo; // bar

But beware that it doesn’t actually change the original class. It just creates a new one. And beware that this requires that the properties are public or have getter and setter magic methods…

And if you wanted some more checks (I’d suggest so), I’d add this line as the first line of castAs to prevent issues:

if (!$newClass instanceof self) {
    throw new InvalidArgumentException(
        'Can\'t change class hierarchy, you must cast to a child class'
    );
}

Alright, since Gordon posted a very black-magic solution, I will do the same (using the RunKit PECL extension (warning: here be dragons):

class myClass {}
class myChildClass extends MyClass {}

function getInstance($classname) {
    //create random classname
    $tmpclass="inheritableClass".rand(0,9);
    while (class_exists($tmpclass))
        $tmpclass .= rand(0,9);
    $code="class ".$tmpclass.' extends '.$classname.' {}';
    eval($code);
    return new $tmpclass();
}

function castAs($obj, $class) {
    $classname = get_class($obj);
    if (stripos($classname, 'inheritableClass') !== 0)
        throw new InvalidArgumentException(
            'Class is not castable'
        );
    runkit_class_emancipate($classname);
    runkit_class_adopt($classname, $class);
}

So, instead of doing new Foo, you’d do something like this:

$obj = getInstance('MyClass');
echo $obj instanceof MyChildClass; //false
castAs($obj, 'myChildClass');
echo $obj instanceof MyChildClass; //true

And from within the class (as long as it was created with getInstance):

echo $this instanceof MyChildClass; //false
castAs($this, 'myChildClass');
echo $this instanceof MyChildClass; //true

Disclaimer: Don’t do this. Really, don’t. It’s possible, but it’s such a horrible idea…

Leave a Comment