美文网首页
stdClass is the default PHP obje

stdClass is the default PHP obje

作者: 张霸天 | 来源:发表于2017-03-24 08:27 被阅读0次

    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.

    相关文章

      网友评论

          本文标题:stdClass is the default PHP obje

          本文链接:https://www.haomeiwen.com/subject/hzyxottx.html