Class Member Access on Instantiation

别等时光非礼了梦想. 提交于 2020-01-04 23:42:11

问题


In PHP 5.4, I believe something like this is valid:

echo ( new DateTime( '2014-04-05 10:36am' ))->format( 'Y-m-d g:ia' );

On PHP 5.3, I currently do something like this:

$date = new DateTime( '2014-04-05 10:36am' );
echo $date->format( 'Y-m-d g:ia' );

Any way to combine those two lines into a single line in PHP 5.3 (and I don't mean by concatenating the lines)? Or will I have to upgrade to >=5.4 to have that option?


回答1:


Will I have to upgrade to >=5.4 to have that option?

Yes. You need to upgrade to PHP 5.4 to do that.

That was a new feature introduced on PHP 5.4 actually.. Class member access on instantiation has been added, e.g. (new Foo)->bar().


If you try doing that on PHP versions less than 5.4 , you will run into this error.

Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';'



来源:https://stackoverflow.com/questions/22890582/class-member-access-on-instantiation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!