问题描述
请定义stdClass
是什么.
stdClass
是PHP的通用空类,类似于Java中的Object
或Python中的object
(但实际上并未用作通用基类;感谢 @Ciaran 指出这一点). /p>
对于匿名对象,动态属性等很有用.
考虑StdClass的一种简单方法是替代关联数组.请参见下面的示例,该示例显示json_decode()
如何允许获取StdClass实例或关联数组.同样在本示例中未显示的SoapClient::__soapCall
返回一个StdClass实例.
<?php
//Example with StdClass
$json = '{ "foo": "bar", "number": 42 }';
$stdInstance = json_decode($json);
echo $stdInstance->foo . PHP_EOL; //"bar"
echo $stdInstance->number . PHP_EOL; //42
//Example with associative array
$array = json_decode($json, true);
echo $array['foo'] . PHP_EOL; //"bar"
echo $array['number'] . PHP_EOL; //42
有关更多示例,请参见 PHP和StdClass中的动态属性. /p>
Please define what stdClass
is.
stdClass
is PHP's generic empty class, kind of like Object
in Java or object
in Python (Edit: but not actually used as universal base class; thanks @Ciaran for pointing this out).
It is useful for anonymous objects, dynamic properties, etc.
An easy way to consider the StdClass is as an alternative to associative array. See this example below that shows how json_decode()
allows to get an StdClass instance or an associative array.Also but not shown in this example, SoapClient::__soapCall
returns an StdClass instance.
<?php
//Example with StdClass
$json = '{ "foo": "bar", "number": 42 }';
$stdInstance = json_decode($json);
echo $stdInstance->foo . PHP_EOL; //"bar"
echo $stdInstance->number . PHP_EOL; //42
//Example with associative array
$array = json_decode($json, true);
echo $array['foo'] . PHP_EOL; //"bar"
echo $array['number'] . PHP_EOL; //42
See Dynamic Properties in PHP and StdClass for more examples.
这篇关于PHP中的stdClass是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!