我不明白代码片段的用途
I don't understand the purpose of code snippet
在 Zend Frameword 2.5 中查看并看到一些代码,它工作正常但我的 IDE 显示错误。
我不知道这段代码的目的。
为什么要写:$this->table = clone $this->table;
Github Link: https://github.com/zendframework/zend-db/blob/master/src/TableGateway/AbstractTableGateway.php
函数:第 529-544 行
请解释一下。
public function __clone()
{
$this->resultSetPrototype = (isset($this->resultSetPrototype)) ? clone $this->resultSetPrototype : null;
$this->sql = clone $this->sql;
if (is_object($this->table)) {
$this->table = clone $this->table;
} elseif (
is_array($this->table)
&& count($this->table) == 1
&& is_object(reset($this->table))
) {
foreach ($this->table as $alias => &$tableObject) {
$tableObject = clone $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
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;
}
}
$a = new B();
$c = clone($a);
$c->setFooBar(3);
$c->setValue(6);
var_dump($a);
echo "\n";
var_dump($c);
?>
clone
(或__clone)是一种所谓的魔术方法。查看 php 文档 on magic methods here.
以获取其他魔术方法的参考
还要检查 the specific documentation for clone
他们在哪里解释了这个魔术方法的工作原理:
An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). An object's __clone() method cannot be called directly.
换句话说,它允许您使用 public __clone
方法在其 class 定义中为对象实例定义自定义克隆行为。当您执行以下操作时,此方法将被调用 "magically":
$clone = clone $instance;
在 Zend Frameword 2.5 中查看并看到一些代码,它工作正常但我的 IDE 显示错误。 我不知道这段代码的目的。 为什么要写:$this->table = clone $this->table;
Github Link: https://github.com/zendframework/zend-db/blob/master/src/TableGateway/AbstractTableGateway.php
函数:第 529-544 行
请解释一下。
public function __clone()
{
$this->resultSetPrototype = (isset($this->resultSetPrototype)) ? clone $this->resultSetPrototype : null;
$this->sql = clone $this->sql;
if (is_object($this->table)) {
$this->table = clone $this->table;
} elseif (
is_array($this->table)
&& count($this->table) == 1
&& is_object(reset($this->table))
) {
foreach ($this->table as $alias => &$tableObject) {
$tableObject = clone $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
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;
}
}
$a = new B();
$c = clone($a);
$c->setFooBar(3);
$c->setValue(6);
var_dump($a);
echo "\n";
var_dump($c);
?>
clone
(或__clone)是一种所谓的魔术方法。查看 php 文档 on magic methods here.
以获取其他魔术方法的参考
还要检查 the specific documentation for clone
他们在哪里解释了这个魔术方法的工作原理:
An object copy is created by using the clone keyword (which calls the object's __clone() method if possible). An object's __clone() method cannot be called directly.
换句话说,它允许您使用 public __clone
方法在其 class 定义中为对象实例定义自定义克隆行为。当您执行以下操作时,此方法将被调用 "magically":
$clone = clone $instance;