簡體   English   中英

方法鏈接和PHP 5.4.x中的新關鍵字返回錯誤

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

我試圖將下面的代碼鏈接到1行中。 純粹用於實驗,看是否可以完成。

這是原始代碼,可以正常工作:

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

但是,在鏈接時,這將返回錯誤:

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

錯誤是:

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

如果PHP 5.4.x允許這種鏈接,為什么它不能按預期工作?

該錯誤實際上恰好表示它:loadHtml返回了一個布爾值(true / false)。 檢查手冊中的返回值。

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

碼:

$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