如何复制另一个类的对象?

时间:2017-08-25 14:29:23

标签: php symfony class object clone

我有一个扩展另一个类的课程。

班级考试

    class Test
    {
      private $id;
      private $name;
      private $age;
      private $number;

      public function getId() {
        return $this->id;
      }

      public function setId($id) {
         $this->id = $id;
         return $this;
      }

      public function getName() {
        return $this->name;
      }

      public function setName($name) {
         $this->name = $name;
         return $this;
      }

      public function getAge() {
        return $this->age;
      }

      public function setAge($age) {
         $this->age = $age;
         return $this;
      }

     public function getNumber() {
        return $this->number;
      }

   public function setNumber($number) {
         $this->number = $number;
         return $this;
      }
    }

Class TestCopy

use Test;  

class TestCopy extends Text
{
}

然后我有一个类Test的对象:

$object = new Test();
$object->setId = 1;
$object->setName = Tom;
$object->setAge = 20;
$object->setNumber = 10;

如何创建TestCopy类的对象(具有相同的属性),并克隆对象$ object的所有值?

我试过克隆:

$objectCopy = clone $object;

但是对象$ objectCopy必须实例化TestCopy类,当我克隆时,它实例化了Test类。

我也尝试过:

foreach (get_object_vars($object) as $key => $name) {
        $objectCopy->$key = $name;
}

但属性是私有的,当我调用函数get_object_vars时,它返回null。 任何的想法?非常感谢!

1 个答案:

答案 0 :(得分:2)

试试这个,一旦你实例化一个对象,你就不能改变这个类(或其他实现细节)

您可以像这样模拟它:

<?php

  class Test
  {
      private $id;
      private $name;
      private $age;
      private $number;

      public function getId() {
         return $this->id;
      }

      public function setId($id) {
         $this->id = $id;
         return $this;
      }

      public function getName() {
        return $this->name;
      }

      public function setName($name) {
         $this->name = $name;
         return $this;
      }

      public function getAge() {
        return $this->age;
      }

      public function setAge($age) {
         $this->age = $age;
         return $this;
      }

      public function getNumber() {
        return $this->number;
      }

      public function setNumber($number) {
         $this->number = $number;
         return $this;
      }

      public function toArray()
      {
         return get_object_vars($this);
      }

  }

  class TestCopy extends Test
  {
      public $fakeAttribute;
  }

  function getTestCopy($object)
  {
    $copy = new TestCopy();

    foreach($object->toArray() as $key => $value) {
       if(method_exists($copy, 'set'.ucfirst($key))) {
          $copy->{'set'.ucfirst($key)}($value);
       }
    }

    return $copy;
  }

$object = new Test();
$object->setId(1);
$object->setName('Tom');
$object->setAge(20);
$object->setNumber(10);

$copy = getTestCopy($object);
$copy->fakeAttribute = 'fake value';

echo "<pre>";
print_r($object->toArray());
print_r($copy->toArray());

输出:

Array
(
    [id] => 1
    [name] => Tom
    [age] => 20
    [number] => 10
)
Array
(
    [fakeAttribute] => fake value
    [id] => 1
    [name] => Tom
    [age] => 20
    [number] => 10
)