Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top