【问题标题】:Copy a class into another将一个类复制到另一个类
【发布时间】:2014-01-16 15:58:07
【问题描述】:

我有

class A
{
    $a;
    $b;
    $c
}

还有

class B
{
    $a;
    $b;
    $e;
    $f
}

如何将所有公共财产从 A 复制到 B?我已经尝试过克隆方法,但它只给了我 2 个对象 A。

有什么方法可以通用吗?

【问题讨论】:

  • 这叫做继承 :) class B extends A
  • ... 或者,如果由于某些原因您不能使 B 类成为 A 类的子类,只需使用组合:添加 $objectOfClassA 作为 B 类的属性。
  • 你确定继承吗? $c 在 B 中不存在

标签: php clone


【解决方案1】:

如果您正在寻找class“复制”,请通过extends 关键字覆盖:

class A
{
   public $a, $b, $c;
}

class B extends A
{
   public $d, $e, $f;
}

-现在你的B 将继承A 的所有非私有属性(所以$a、$b 和$c 也是如此)

但如果它是关于对象的(例如,你的类无论如何都不相关) - 然后使用 get_object_vars() 进行迭代:

class A
{
    public $a=1;
    public $b=2;
    public $c=3;
}

class B
{
    public $a=5;
    public $b=6;
    public $e=7;
    public $f=8;
}

$foo = new A;
$bar = new B;

foreach(get_object_vars($foo) as $name=>$value)
{
    if(property_exists($bar, $name))
    {
        $bar->$name = $value;
    }
}

-检查fiddle。请注意,属性必须是可见的(公共)才能执行此操作。

【讨论】:

  • 哦,谢谢,这正是我所期望的!如果属性是私有的,但有公共的 get_class_methods 的 getter 和 setter,是否仍然有可能?
  • 通常,您不应该对私有/公共属性执行此操作。但是 - 是的,如果出于某种原因需要这样做,您将不得不使用 setter - 并且,也许,Reflection API
【解决方案2】:

第一种方式(继承)http://www.php.net/manual/en/language.oop5.inheritance.php

<?php

class A {
    public $_x = 'x';
    public $_y = 'y';
    public $_z = 'zz';
}

class B extends A {

    public function __construct() {
        echo $this->_x . "__" . $this->_y . "__" . $this->_z;
    }
}

$b = new B; // x__y__zz

?>

第二种方式 - 类实例化 - 属性是公共的,因此您可以从对象实例中访问它们的值,并将其分配给您的内部属性

<?php

class AA {
    public $_x = 'x';
    public $_y = 'y';
    public $_z = 'zz';
}

class BB {
    public $_x, $_y, $_z;
    private $_AA;
    public function __construct() {
        $this->_AA = new AA();
        $this->_x = $this->_AA->_x;
        $this->_y = $this->_AA->_y;
        $this->_z = $this->_AA->_z;
        echo $this->_x . "__" . $this->_y . "__" . $this->_z;
    }
}

$bb = new BB; // x__y__zz

?>

第三种方式,如果属性是私有的,如果你可以直接访问基类,你可以对 then 进行访问,所以即使它们不能被外部覆盖,它们的值也可以访问

<?php

class AAA {
    private $_x = 'x';
    private $_y = 'y';
    private $_z = 'zz';

    public function getX() {
        return $this->_x;
    }
    public function getY() {
        return $this->_y;
    }
    public function getZ() {
        return $this->_z;
    }
}

class BBB {
    public $_x, $_y, $_z;
    private $_AAA;
    public function __construct() {
        $this->_AAA = new AAA();
        $this->_x = $this->_AAA->getX();
        $this->_y = $this->_AAA->getY();
        $this->_z = $this->_AAA->getZ();
        echo $this->_x . "__" . $this->_y . "__" . $this->_z;
    }
}

$bbb = new BBB; // x__y__zz

通用:)

<?php

class AAA {
    private $_x = 'x';
    private $_y = 'y';
    private $_z = 'zz';

    public function getX() {
        return $this->_x;
    }
    public function getY() {
        return $this->_y;
    }
    public function getZ() {
        return $this->_z;
    }
}

class BBB {
    public $_x, $_y, $_z;
}

$AAA = new AAA();
$BBB = new BBB();
$get = 'get';
$arr_AAA = (array)$AAA;
foreach($arr_AAA as $key => $value) {
    $property = explode('_', $key);
    $property = ucfirst($property[1]);
    $getter[] = $get.$property;
}
$i = 0;
foreach (get_object_vars($BBB) as $k=>$v) {
        $get = $getter[$i];
        $BBB->$k = $AAA->$get();
        $i++;
}

var_dump($BBB);
/**
object(BBB)[2]
  public '_x' => string 'x' (length=1)
  public '_y' => string 'y' (length=1)
  public '_z' => string 'zz' (length=2)
 * 
 */

这应该适用于即

private $_qwe = 'qwe';
public getQwe() {
   return $this->_qwe;
}

它将第一个字母大写,因为它应该是 getter 的约定。当然,您可以建立自己的约定。

【讨论】:

  • @Royal_BG 你的代码认为我所有的变量都是一些字符串
  • @goto 这不是真的。你可以有$_this-&gt;qwe = new SomeClass()。之后qwe 的副本将是object(BBB)[3] public '_qwe' =&gt; object(SomeClass)[2] public 'x' =&gt; null 刚刚测试过的。无论您的属性包含什么,它们都将被复制
猜你喜欢
  • 2010-11-15
  • 2012-03-06
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2013-03-18
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多