本文介绍了PHP中的stdClass是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请定义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是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 23:16