22

I have a parent class A, and child class B in PHP. Is there any way to clone instance of class A to instance of B, and to use B class properties later in B instance?

4

2 Answers 2

25

My solution will be based on the solution from this question How do you copy a PHP object into a different object type

class childClass extends parentClass
{
    private $a;
    private $b;

    function loadFromParentObj( $parentObj )
    {
        $objValues = get_object_vars($parentObj); // return array of object values
        foreach($objValues AS $key=>$value)
        {
             $this->$key = $value;
        }
    }
}

$myParent = new parentClass();
$myChild = new childClass();
$myChild->loadFromParentObj( $myParent );
Sign up to request clarification or add additional context in comments.

Comments

0

1. Simple option to understand. But in case of changes in ParentClass properties, there is a possibility of "missing" the corresponding changes in ChildClass

<?php
class ParentClass {
    public function __construct (
        public string $propParent1,
        public string $propParent2,
    ) {}
}

class ChildClass extends ParentClass{
    public string $propChild;
    
    public static function createFromParentClass(ParentClass $parentObj): ChildClass
    {
        // there is a possibility of "missing" the changes in ParentClass
        $instance = new static(
            $parentObj->propParent1, 
            $parentObj->propParent2
        );
        
        $instance->propChild = substr($instance->propParent1, 0, 5);
        
        return $instance;
    }
}

$parentObj = new ParentClass('Parent property', 'test');
$childObj = ChildClass::createFromParentClass($parentObj);
var_dump($parentObj);
var_dump($childObj);

2. The cloneParentClass pseudo-cloning method is used. All changes to the ParentClass properties are processed immediately in this class.

<?php
class ParentClass {
    public function __construct (
        public string $propParent1,
        public string $propParent2,
    ) {}
    
    /*Fatal error: Declaration of ChildClass::createFromParentClass(ParentClass $parentObj): ChildClass must be compatible with ParentClass::createFromParentClass(ParentClass $parentObj): static in /home/user/scripts/code.php on line 22
    */
    //public static function createFromParentClass(self $parentObj): static        
    public static function cloneParentClass(self $parentObj): static
    {
        // повне дублювання clone
        $instance = new static(
            $parentObj->propParent1,
            $parentObj->propParent2
        );
        return $instance;
    }
}


class ChildClass extends ParentClass{
    public string $propChild;
    
    public static function createFromParentClass(ParentClass $parentObj): ChildClass
    {
        $instance = parent::cloneParentClass($parentObj);
        $instance->propChild = substr($instance->propParent1, 0, 5);
        return $instance;
    }
}

$parentObj = new ParentClass('Parent property', 'test');
$childObj = ChildClass::createFromParentClass($parentObj);

var_dump($parentObj);
var_dump($childObj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.