编程语言
首页 > 编程语言> > 什么是PHP中的stdClass?

什么是PHP中的stdClass?

作者:互联网

请定义stdClass是什么.

解决方法:

stdClass是PHP的通用空类,有点像Java中的Object或Python中的对象(编辑:但实际上并不用作通用基类;感谢@Ciaran为pointing this out).

它对匿名对象,动态属性等很有用.

考虑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

有关更多示例,请参见Dynamic Properties in PHP and StdClass.

标签:stdclass,php
来源: https://codeday.me/bug/20190911/1803139.html