Does PHP feature short hand syntax for objects?

前端 未结 7 655
囚心锁ツ
囚心锁ツ 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:40

    As of PHP 5.4, you can do this:

    $array = ['foo'=>'bla', 'bar'=>2];
    

    It's not much shorter, but you'll appreciate it if you need to use a lot of hard coded nested arrays (which isn't altogether uncommon).

    If you want an object, you would still need to cast each array:

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

提交回复
热议问题