我可以将 PHP 对象传递给同一对象的构造函数吗?
Can I pass a PHP object to a constructor for that same object?
(所以结束了我之前的问题,所以这次我会尽量说得更清楚)
当我 运行 在浏览器中输入以下代码时:
<?php
class MyNode {
public $name;
public $children = array();
public $parent;
public function __construct(string $name, $parent) {
echo "in constructor\n";
$this->name = $name;
echo "name: " . $this->name . ".\n";
$this->parent = $parent;
#$this->parent = &$parent;
echo "parent: " . $this->parent . ".\n";
}
}
$RootNode = new MyNode("Root", null);
echo "done root x\n";
#$ChildNode = new MyNode("Child1", null);
$ChildNode = new MyNode("Child1", $RootNode);
echo "done child\n";
?>
浏览器打印“in constructor name: Root.parent: . done root x in constructor name: Child1.”然后因错误(在开发人员控制台中)“500(内部服务器错误)”而停止。如果我使用传递 null 而不是 $RootNode 的行(上面注释),它会成功。
将 $RootNode 传递给 MyNode 构造函数的正确方法是什么?
你非常接近 - if you had error reporting on,问题会很清楚。您的代码在尝试打印 $this->parent
时发出错误,因为 PHP 无法将 MyNode
的实例转换为字符串 - PHP 需要这样做才能打印它。
Here's an example of your code. 它发出此错误:
Fatal error: Uncaught Error: Object of class MyNode could not be converted to string in /in/T7Os5:12
Stack trace:
#0 /in/T7Os5(18): MyNode->__construct('Child1', Object(MyNode))
#1 {main}
thrown in /in/T7Os5 on line 12
我建议实施 __toString()
method (example):
<?php
class MyNode {
public $name;
public $children = array();
public $parent;
public function __construct(string $name, $parent) {
echo "in constructor\n";
$this->name = $name;
echo "name: " . $this->name . ".\n";
$this->parent = $parent;
echo "parent: " . $this->parent . ".\n";
}
public function __toString()
{
return $this->name;
}
}
$RootNode = new MyNode("Root", null);
echo "done root x\n";
$ChildNode = new MyNode("Child1", $RootNode);
echo "done child\n";
(所以结束了我之前的问题,所以这次我会尽量说得更清楚) 当我 运行 在浏览器中输入以下代码时:
<?php
class MyNode {
public $name;
public $children = array();
public $parent;
public function __construct(string $name, $parent) {
echo "in constructor\n";
$this->name = $name;
echo "name: " . $this->name . ".\n";
$this->parent = $parent;
#$this->parent = &$parent;
echo "parent: " . $this->parent . ".\n";
}
}
$RootNode = new MyNode("Root", null);
echo "done root x\n";
#$ChildNode = new MyNode("Child1", null);
$ChildNode = new MyNode("Child1", $RootNode);
echo "done child\n";
?>
浏览器打印“in constructor name: Root.parent: . done root x in constructor name: Child1.”然后因错误(在开发人员控制台中)“500(内部服务器错误)”而停止。如果我使用传递 null 而不是 $RootNode 的行(上面注释),它会成功。
将 $RootNode 传递给 MyNode 构造函数的正确方法是什么?
你非常接近 - if you had error reporting on,问题会很清楚。您的代码在尝试打印 $this->parent
时发出错误,因为 PHP 无法将 MyNode
的实例转换为字符串 - PHP 需要这样做才能打印它。
Here's an example of your code. 它发出此错误:
Fatal error: Uncaught Error: Object of class MyNode could not be converted to string in /in/T7Os5:12
Stack trace:
#0 /in/T7Os5(18): MyNode->__construct('Child1', Object(MyNode))
#1 {main}
thrown in /in/T7Os5 on line 12
我建议实施 __toString()
method (example):
<?php
class MyNode {
public $name;
public $children = array();
public $parent;
public function __construct(string $name, $parent) {
echo "in constructor\n";
$this->name = $name;
echo "name: " . $this->name . ".\n";
$this->parent = $parent;
echo "parent: " . $this->parent . ".\n";
}
public function __toString()
{
return $this->name;
}
}
$RootNode = new MyNode("Root", null);
echo "done root x\n";
$ChildNode = new MyNode("Child1", $RootNode);
echo "done child\n";