When trying to do this in PHP 5.2.9:
$foo = (clone $template)->bar();
PHP gives me a syntax error:
Parser error "';' expected after expression (Found token: ->)"
Am I doing something wrong? or is there simply no way to clone an object inline, such that I would have to split my statement into two lines?
Unfortunately, PHP does not allow that syntax (in any version). As an alternative to breaking it into two lines, you can do this:
$foo = call_user_func(array(clone $template, 'bar'));
class X {
public function foo(){
echo 'inline clone';
}
}
$x = new X;
$y = clone $x and $y->foo(); // "inline clone"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With