@ 2016-06-08T13:47:38Z <?php
class ReflectionHelper
{
public static function getProperties(\ReflectionClass $ref)
{
$props = $ref->getProperties();
$propsArr = array();
foreach ($props as $prop) {
$f = $prop->getName();
$propsArr[$f] = $prop;
}
if ($parentClass = $ref->getParentClass()) {
$parentPropsArr = self::getProperties($parentClass);
if (count($parentPropsArr) > 0) {
$propsArr = array_merge($parentPropsArr, $propsArr);
}
}
return $propsArr;
}
}
class DeepCopy
{
/**
* Perform a deep copy of the object.
* @param mixed $object
* @return mixed
*/
public function copy($object)
{
$this->hashMap = [];
return $this->recursiveCopy($object);
}
private function recursiveCopy($var)
{
// Resource
if (is_resource($var)) {
return $var;
}
// Array
if (is_array($var)) {
return $this->copyArray($var);
}
// Scalar
if (! is_object($var)) {
return $var;
}
// Object
return $this->copyObject($var);
}
/**
* Copy an array
* @param array $array
* @return array
*/
private function copyArray(array $array)
{
$copier = function($item) {
return $this->recursiveCopy($item);
};
return array_map($copier, $array);
}
/**
* Copy an object
* @param object $object
* @return object
*/
private function copyObject($object)
{
$reflectedObject = new \ReflectionObject($object);
$newObject = clone $object;
foreach (ReflectionHelper::getProperties($reflectedObject) as $property) {
$this->copyObjectProperty($newObject, $property);
}
return $newObject;
}
private function copyObjectProperty($object, ReflectionProperty $property)
{
// Ignore static properties
if ($property->isStatic()) {
return;
}
$property->setAccessible(true);
$propertyValue = $property->getValue($object);
// Copy the property
$property->setValue($object, $this->recursiveCopy($propertyValue));
}
}
class TestObject {
private $name;
private $startDate;
public function __construct() {
$this->name = "Peter";
$this->startDate = new DateTime("2016-04-05");
}
}
function copyObjectProperty($object, ReflectionProperty $property) {
// Ignore static properties
if ($property->isStatic()) {
return;
}
$property->setAccessible(true);
$propertyValue = $property->getValue($object);
// Copy the property
$property->setValue($object, recursiveCopy($propertyValue));
}
$object = new TestObject();
$deepCopy = new DeepCopy();
$newObject = $deepCopy->copy($object);
var_dump($newObject);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.2.0 - 8.2.19 , 8.3.0 - 8.3.7 Deprecated: Creation of dynamic property DeepCopy::$hashMap is deprecated in /in/NOGDr on line 34
object(TestObject)#5 (2) {
["name":"TestObject":private]=>
string(5) "Peter"
["startDate":"TestObject":private]=>
object(DateTime)#9 (3) {
["date"]=>
string(26) "2016-04-05 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
}
Output for 5.4.30 - 5.4.45 , 5.5.14 - 5.5.38 , 5.6.0 - 5.6.28 , 7.0.0 - 7.0.6 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.12 , 8.0.14 - 8.0.30 , 8.1.0 - 8.1.28 object(TestObject)#5 (2) {
["name":"TestObject":private]=>
string(5) "Peter"
["startDate":"TestObject":private]=>
object(DateTime)#9 (3) {
["date"]=>
string(26) "2016-04-05 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
}
Output for 7.3.32 - 7.3.33 , 8.0.13 object(TestObject)#5 (2) {
["name":"TestObject":private]=>
string(5) "Peter"
["startDate":"TestObject":private]=>
object(DateTime)#9 (3) {
["date"]=>
string(26) "2016-04-05 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
}
Output for 7.0.7 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.6 - 7.2.33 , 7.3.16 - 7.3.31 Notice: Undefined property: DateTime::$date in /in/NOGDr on line 98
Notice: Undefined property: DateTime::$timezone_type in /in/NOGDr on line 98
Notice: Undefined property: DateTime::$timezone in /in/NOGDr on line 98
object(TestObject)#5 (2) {
["name":"TestObject":private]=>
string(5) "Peter"
["startDate":"TestObject":private]=>
object(DateTime)#9 (3) {
["date"]=>
string(26) "2016-04-05 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
}
Output for 5.4.0 - 5.4.29 , 5.5.0 - 5.5.13 object(TestObject)#5 (2) {
["name":"TestObject":private]=>
string(5) "Peter"
["startDate":"TestObject":private]=>
object(DateTime)#9 (3) {
["date"]=>
string(19) "2016-04-05 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
}
Output for 5.3.0 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/NOGDr on line 34
Process exited with code 255 . Output for 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/NOGDr on line 4
Parse error: syntax error, unexpected '[' in /in/NOGDr on line 34
Process exited with code 255 . Output for 5.0.0 - 5.0.5 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/NOGDr on line 4
Parse error: parse error, unexpected '[' in /in/NOGDr on line 34
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/NOGDr on line 4
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/NOGDr on line 4
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/NOGDr on line 4
Process exited with code 255 . preferences:dark mode live preview
168.4 ms | 401 KiB | 353 Q