php如何创建没有类的对象

时间:2017-07-28
本文章向大家介绍php如何创建没有类的对象,需要的朋友可以参考一下

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

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

对于PHP,该如何实现呢?

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

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

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

   var_dump($object);
   /*
   outputs:

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

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

$object = (object) ['property' => 'Here we go'];