我正在查看这个单链表的实现,并注意到在构造函数内部,属性$this->next
被设置为null
。
由于php自动设置属性$next
的值为null
时,它是在构造函数外声明(如public $next
),是不是行代码$this->next = NULL
过度杀死?
class ListNode
{
public $data;
public $next;
function __construct($data)
{
$this->data = $data;
$this->next = NULL;
}
function readNode()
{
return $this->data;
}
}
也-我看到这个约定在php OOP中被使用了好几次。这在其他语言中是必需的约定吗?
$this->next已经是NULL了,所以没有必要重新声明。