Home » Php » php – Cast the current object ($this) to a descendent class

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a class where it may be necessary to change the object to a descendent class further down the line. Is this possible? I know that one option is to return a copy of it but using the child class instead, but it’d be nice to actually modify the current object… so:

class myClass {
  protected $var;

  function myMethod()
  {
    // function which changes the class of this object
    recast(myChildClass); 
  }
}

class myChildClass extends myClass {
}

$obj = new myClass();
$obj->myMethod();
get_class_name($obj); // => myChildClass
How to&Answers:

You can, as described in other answers, do it with nasty black magic PECL extensions.

Though, you seriously don’t want it. Any problem you want to solve in OOP there’s an OOP-compliant way to do it.

Runtime type hierarchy modifications are not OOP-compliant (in fact, this is consciously avoided). There are design patterns that should fit what you want.

Please, tell us why do you want that, I’m sure there must be better ways to do it 😉

Answer:

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…

Answer:

Redefining Classes

You can do this with the runkit PECL extension aka the “Toolkit from Hell”:

  • runkit_class_adopt — Convert a base class to an inherited class, add ancestral methods when appropriate
  • runkit_class_emancipate — Convert an inherited class to a base class, removes any method whose scope is ancestral

Redefining Instances

The runkit functions do not work on object instances. If you want to do that on object instances, you could theoretically do that by messing with the serialized object strings.
This is the realms of black magic though.

The code below allows you to change an instance to whatever other class:

function castToObject($instance, $className)
{
    if (!is_object($instance)) {
        throw new InvalidArgumentException(
            'Argument 1 must be an Object'
        );
    }
    if (!class_exists($className)) {
        throw new InvalidArgumentException(
            'Argument 2 must be an existing Class'
        );
    }
    return unserialize(
        sprintf(
            'O:%d:"%s"%s',
            strlen($className),
            $className,
            strstr(strstr(serialize($instance), '"'), ':')
        )
    );
}

Example:

class Foo
{
    private $prop1;
    public function __construct($arg)
    {
        $this->prop1 = $arg;
    }
    public function getProp1()
    {
        return $this->prop1;
    }
}
class Bar extends Foo
{
    protected $prop2;
    public function getProp2()
    {
        return $this->prop2;
    }
}
$foo = new Foo('test');
$bar = castToObject($foo, 'Bar');
var_dump($bar);

Result:

object(Bar)#3 (2) {
  ["prop2":protected]=>
  NULL
  ["prop1":"Foo":private]=>
  string(4) "test"
}

As you can see, the resulting object is a Bar object now with all properties retaining their visibility but prop2 is NULL. The ctor doesnt allow this, so technically, while you have a Bar child of Foo, it is not in a valid state. You could add a magic __wakeup method to handle this somehow, but seriously, you dont want that and it shows why casting is ugly business.

DISCLAIMER: I absolutely do not encourage anyone to use any of these solutions in production.

Answer:

This is not possible because while an instance of a child class is also an instance of a parent class, the reverse is not true.

What you could do is create a new instance of the child class and copy the values from the old object onto it. You can then return the new object which will be of type myChildClass.

Answer:

For simple classes this may work (I am using this successfully in some rare cases):

function castAs($sourceObject, $newClass)
{
    $castedObject                    = new $newClass();
    $reflectedSourceObject           = new \ReflectionClass($sourceObject);
    $reflectedSourceObjectProperties = $reflectedSourceObject->getProperties();

    foreach ($reflectedSourceObjectProperties as $reflectedSourceObjectProperty) {
        $propertyName = $reflectedSourceObjectProperty->getName();

        $reflectedSourceObjectProperty->setAccessible(true);

        $castedObject->$propertyName = $reflectedSourceObjectProperty->getValue($sourceObject);
    }
}

Usage in my case:

$indentFormMapper = castAs($formMapper, IndentedFormMapper::class);

More abstract:

$castedObject = castAs($sourceObject, TargetClass::class);

Of course TargetClass has to inherit from the class of sourceObject and you have to make all protected and private properties public in TargetClass to get this work.

I use this to change FormMapper (https://github.com/sonata-project/SonataAdminBundle/blob/3.x/src/Form/FormMapper.php) on the fly to IndentedFormMapper by adding a new method called chain:

class IndentedFormMapper extends FormMapper
{
    /**
     * @var AdminInterface
     */
    public $admin;

    /**
     * @var BuilderInterface
     */
    public $builder;

    /**
     * @var FormBuilderInterface
     */
    public $formBuilder;

    /**
     * @var string|null
     */
    public $currentGroup;

    /**
     * @var string|null
     */
    public $currentTab;

    /**
     * @var bool|null
     */
    public $apply;

    public function __construct()
    {
    }

    /**
     * @param $callback
     * @return $this
     */
    public function chain($callback)
    {
        $callback($this);

        return $this;
    }
}