胖鸭 2009-05-31 05:49
浏览 446
已采纳

什么是 PHP 中的 stdClass?

Please define what stdClass is.

转载于:https://stackoverflow.com/questions/931407/what-is-stdclass-in-php

  • 写回答

16条回答 默认 最新

  • 程序go 2009-05-31 05:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(15条)

报告相同问题?

悬赏问题

  • ¥15 html5+css有人可以帮吗?
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?