PHP stdClass类

PHP stdClass类

stdClass是PHP的泛型空类,类似于Java中的Object或Python中的Object(编辑:但实际上不用作通用基类)。

它对匿名对象、动态属性等都很有用。

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

(0)
上一篇 2020年10月19日 上午10:37
下一篇 2020年10月19日 上午11:19

相关推荐

发表回复

登录后才能评论