实例化时的类成员访问 [英] Class Member Access on Instantiation

查看:65
本文介绍了实例化时的类成员访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP 5.4中,我相信这样是有效的:

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' );

在PHP 5.3上,我目前正在执行以下操作:

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' );

在PHP 5.3中将这两行合并为一行的任何方法(我并不是说通过连接线)?还是我必须升级到> = 5.4才能拥有该选项?

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?

推荐答案


我将拥有升级到> = 5.4才能拥有该选项?

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

是。。您需要升级到PHP 5.4做到这一点。

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

那是 新功能 实际上是在PHP 5.4上引入的。.
实例化时的类成员访问已添加,例如(新Foo)-> bar()。

如果您尝试在PHP版本低于 5.4 ,您将遇到此错误。

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


解析错误:语法错误,T_OBJECT_OPERATOR意外,期望为', '
或';'

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

这篇关于实例化时的类成员访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆