将课程复制到另一个课程

时间:2014-01-16 15:58:07

标签: php clone

我有

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

并且

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

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

有没有办法一般地做到这一点?

2 个答案:

答案 0 :(得分: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。请注意,这些属性必须是可见的(公共)才能执行此操作。

答案 1 :(得分:-1)

第一种方式(继承)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

?>

第三种方式,如果属性是私有的,并且你可以直接访问基类,那么你可以创建访问者,所以即使它们不能从外部覆盖,也可以访问它们的值

<?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的约定。当然,你可以建立自己的约定。