Search code examples
phpclasscloneinstance

Copying an instance of a PHP class while preserving the data in a base class?


I have the following three classes:

class a
{ public $test; }

class b extends a { }
class c extends a
{
    function return_instance_of_b() { }
}

As you can see, both classes b and c derive from a. In the return_instance_of_b() function in c, I want to return an instance of the class b. Basically return new b(); with one additional restriction:

I need the data from the base class (a) to be copied into the instance of b that is returned. How would I go about doing that? Perhaps some variant of the clone keyword?


Solution

  • You can use the get_class_vars function to retrieve the names of the variables you want to copy, and just loop to copy them.

    The variables that are defined are protected so they are visible to get_class_vars in its scope (since c extends a), but not directly accessible outside the class. You can change them to public, but private will hide those variables from get_class_vars.

    <?php
    class a
    { 
        protected $var1;
        protected $var2;
    }
    
    class b extends a 
    {
    }
    
    class c extends a
    {
        function __construct()
        {
            $this->var1 = "Test";
            $this->var2 = "Data";
        }
        
        function return_instance_of_b() 
        {
            $b = new b();
            // Note: get_class_vars is scope-dependant - It will not return variables not visible in the current scope
            foreach( get_class_vars( 'a') as $name => $value) {
                $b->$name = $this->$name;
            }
            return $b;
        }
    }
    
    $c = new c();
    $b = $c->return_instance_of_b();
    var_dump( $b); // $b->var1 = "Test", $b->var2 = "Data