Zend framework2 我不知道';我不理解代码片段的用途
在Zend Frameword 2.5中,我查看了一些代码,它工作正常,但我的IDE显示了错误。 我不知道这段代码的目的。 为什么要写:$this->table=clone$this->table Github链接: 功能:第529-544行 请给我解释一下Zend framework2 我不知道';我不理解代码片段的用途,zend-framework2,Zend Framework2,在Zend Frameword 2.5中,我查看了一些代码,它工作正常,但我的IDE显示了错误。 我不知道这段代码的目的。 为什么要写:$this->table=clone$this->table Github链接: 功能:第529-544行 请给我解释一下 public函数 { $this->resultSetPrototype=(isset($this->resultSetPrototype))?克隆$this->resultSetPrototype:null; $this->sql=克隆$
public函数
{
$this->resultSetPrototype=(isset($this->resultSetPrototype))?克隆$this->resultSetPrototype:null;
$this->sql=克隆$this->sql;
如果(是对象($this->table)){
$this->table=克隆$this->table;
}埃尔塞夫(
is_数组($this->table)
&&计数($this->table)==1
&&is_对象(重置($this->table))
) {
foreach($alias=>&$tableObject形式的此->表){
$tableObject=克隆$tableObject;
}
}
}
我无法理解Zend的用途,但我希望在运行下面的两段代码片段后,从不同的两个结果中,您可以理解
<?php
class A {
public $foo = 1;
}
class B {
protected $value = 1;
protected $bar = null;//
public function __construct() {
$this->bar = new A();
}
public function setValue($foo = 3){
$this->value = $foo;
}
public function setFooBar($foo = 3){
$this->bar->foo = $foo;
}
public function __clone() {
$this->bar = clone($this->bar);
}
}
$a = new B();
$c = clone($a);
$c->setFooBar(3);
$c->setValue(6);
var_dump($a);
echo "\n";
var_dump($c);
?>
克隆
(或克隆)是一种所谓的神奇方法。在php文档中查看其他魔术方法的参考。同时检查他们在哪里解释了这种神奇方法的工作原理: 使用clone关键字创建对象副本(如果可能,该关键字将调用对象的_clone()方法)。无法直接调用对象的_clone()方法 换句话说,它允许您使用public
\uu clone
方法在对象实例的类定义中定义自定义克隆行为。当您执行以下操作时,将“神奇地”调用此方法:
$clone = clone $instance;
你不明白我的问题。这里的问题是我想知道的是为什么,而不是什么。
$clone = clone $instance;