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
-
1php.net/manual/en/language.oop5.cloning.phpdavidkonrad– davidkonrad2012-10-14 12:42:51 +00:00Commented Oct 14, 2012 at 12:42
-
1I don't think you can extend instances in real time if that is what you are asking. Consider giving more details about what you are trying to do though, perhaps there is another way.Mahn– Mahn2012-10-14 12:47:08 +00:00Commented Oct 14, 2012 at 12:47
-
Does this answer your question? How do you copy a PHP object into a different object typeMelebius– Melebius2022-07-07 06:37:36 +00:00Commented Jul 7, 2022 at 6:37
-
For those looking for answers to this question, here are the best answers - stackoverflow.com/questions/41958315/…tecdoc ukr net– tecdoc ukr net2024-01-21 17:19:55 +00:00Commented Jan 21, 2024 at 17:19
Add a comment
|
2 Answers
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 );
Comments
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);