如何在 PHP 中定义一个空对象

如何在 PHP 中定义一个空对象

问题描述:

用一个新数组我这样做:

with a new array I do this:

$aVal = array();

$aVal[key1][var1] = "something";
$aVal[key1][var2] = "something else";

是否有类似的对象语法

(object)$oVal = "";

$oVal->key1->var1 = "something";
$oVal->key1->var2 = "something else";

$x = new stdClass();

手册中的注释总结最好的:

stdClass 是默认的 PHP 对象.stdClass 没有属性、方法或父母.不支持魔法方法,并且不实现任何接口.

stdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces.

当您将标量或数组转换为对象,你得到一个实例标准类.你可以使用标准类每当您需要通用对象时实例.

When you cast a scalar or array as Object, you get an instance of stdClass. You can use stdClass whenever you need a generic object instance.