简体   繁体   English

方法链接和PHP 5.4.x中的新关键字返回错误

[英]Method Chaining and the new Keyword returning error in PHP 5.4.x

I was trying to chain the following code below into 1 line. 我试图将下面的代码链接到1行中。 Purely for experimentation to see if it could be done. 纯粹用于实验,看是否可以完成。

This is the original code, which works: 这是原始代码,可以正常工作:

$dom = new DomDocument();
$dom->loadHtml($html);
$xpath = new DomXPath($dom);

But, when chaining, this returns an error: 但是,在链接时,这将返回错误:

$xpath = new DomXPath((new DomDocument())->loadHtml($html));

The error is: 错误是:

Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, boolean given

If this sort of chaining is allowed in PHP 5.4.x the why does it not work as expected? 如果PHP 5.4.x允许这种链接,为什么它不能按预期工作?

The error actually said it just perfeclly: loadHtml returned a Bool (true/false). 该错误实际上恰好表示它:loadHtml返回了一个布尔值(true / false)。 Check the manual for the return value. 检查手册中的返回值。

Error: Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, boolean given 错误: Catchable fatal error: Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, boolean given

Code: 码:

$xpath = new DomXPath((new DomDocument())->loadHtml($html));
//                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     Argument 1, that returned a bool.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM