stdClass is the default PHP object.

stdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces.

When you cast a scalar or array as Object, you get an instance of stdClass. You can use stdClass whenever you need a generic object instance.


<?php
// ways of creating stdClass instances
$x = new stdClass;
$y = (object) null;        // same as above
$z = (object) 'a';         // creates property 'scalar' = 'a'
$a = (object) array('property1' => 1, 'property2' => 'b');
?>

stdClass is NOT a base class! PHP classes do not automatically inherit from any class. All classes are standalone, unless they explicitly extend another class. PHP differs from many object-oriented languages in this respect.


<?php
// CTest does not derive from stdClass
class CTest {
    public $property1;
}
$t = new CTest;
var_dump($t instanceof stdClass);            // false
var_dump(is_subclass_of($t, 'stdClass'));    // false
echo get_class($t) . "\n";                   // 'CTest'
echo get_parent_class($t) . "\n";            // false (no parent)
?>

You cannot define a class named 'stdClass'in your code. That name is already used by the system. You can define a class named 'Object'.

You could define a class that extends stdClass, but you would get no benefit, as stdClass does nothing.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • The Insider's Guide to PHP InterviewingUbiquitous…that is...
    Yves_lau阅读 526评论 1 2
  • 就像 我一次次路过有你的梦 却不能敲开你的夜晚 量你踩过的脚印 却不敢看你的脸 我猜 你又会在今天的海边 看浪花仍...
    绘莲氏阅读 226评论 0 5
  • 回忆的大门被开启 ——读绘本《棉婆婆睡不着》 去年正月和女儿一起读《棉婆婆睡不着》,心里暖暖的,记忆如春风般掠过脑...
    舴艨载愁阅读 269评论 0 0
  • 今天看牡丹去了,好累!不过还要坚持画画啦。简笔画表情一组,喜欢的可以收藏,加上头发和耳朵,人物就完整了,可以自己动...
    影子倒了阅读 468评论 8 18
  • 搬家加上过年,有四次没有参加公众号“系统学管理”的每周学习。不过看到群里,还是有不少同修每周都按时提交作业,并有精...
    德敏阅读 1,266评论 0 1