Does PHP feature short hand syntax for objects?

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

    According to the new PHP syntaxes,

    You can use

    $array = [1,2,3];
    

    And for associative arrays

    $array = ['name'=>'Sanket','age'=>22];
    

    For objects you can typecast the array to object

    $object = (object)['name'=>'Sanket','age'=>22];
    

提交回复
热议问题