Creating anonymous objects in php

前端 未结 12 1881
故里飘歌
故里飘歌 2020-11-28 01:59

As we know, creating anonymous objects in JavaScript is easy, like the code below:

var object = { 
    p : \"value\", 
    p1 : [ \"john\", \"johnny\" ]
};

         


        
12条回答
  •  有刺的猬
    2020-11-28 02:41

    Support for anonymous classes has been available since PHP 7.0, and is the closest analogue to the JavaScript example provided in the question.

    p1[1];
    

    The visibility declaration on properties cannot be omitted (I just used var because it's shorter than public.)

    Like JavaScript, you can also define methods for the class:

    p;}
    };
    
    echo $object->foo();
    

提交回复
热议问题