Does PHP feature short hand syntax for objects?

前端 未结 7 664
囚心锁ツ
囚心锁ツ 2020-12-06 04:04

In javascript you can easily create objects and Arrays like so:

var aObject = { foo:\'bla\', bar:2 };
var anArray = [\'foo\', \'bar\', 2];

7条回答
  •  情书的邮戳
    2020-12-06 04:31

    There was a proposal to implement this array syntax. But it was declined.


    Update    The shortened syntax for arrays has been rediscussed, accepted, and is now on the way be released with PHP 5.4.

    But there still is no shorthand for objects. You will probably need to explicitly cast to object:

    $obj = (object) ['foo'=>'bla', 'bar'=>2];
    

提交回复
热议问题