在 中,您可以通过以下方式轻松创建没有类的对象:

 myObj = {};
 myObj.abc = \"aaaa\";

对于PHP,该如何实现呢?

在php中,我们可以使用new stdClass()。

你可以随时使用new stdClass()。示例代码:

   $  = new stdClass();
   $ ->property = \'Here we go\';

   var_dump($ );
   /*
   outputs:

    (stdClass)#2 (1) {
      [\"property\"]=>
      string(10) \"Here we go\"
    }
   */

从PHP 5.4开始,您可以获得相同的输出:

$  = ( ) [\'property\' => \'Here we go\'];
收藏 打印