For learning purposes, is there any way of initializing an object, call its method and assign the object to the variable in one statement in PHP?
I've tried something like:
$dom = (new DOMDocument)->loadHTML('<html></html>');
echo gettype($dom); // Result: boolean
but it's returning boolean instead of the object, so I can't do anything with it.
I'm expecting to be an object, so I can call: $dom->saveHTML();
, but instead it's throwing the error:
Fatal error: Call to a member function saveHTML() on a non-object
Is there any workaround? Or these are the limits of the language.
The following code works correctly:
$dom = new DOMDocument;
$dom->loadHTML('<html></html>');
echo gettype($dom); // Result: object
I'm using PHP 5.5.3 (cli).