Home > Article > Backend Development > New stdClass internal reserved class in PHP5
The stdClass class is an internal reserved class of PHP. Initially, there are no member variables or member methods. All magic methods are set to NULL. You can use it to pass variable parameters, but there are no methods that can be called. The stdClass class can be inherited, but there is little point in doing so.
This class is a reserved class of PHP and is not the base class of all classes.
<?php class foo {} $bar = new foo(); echo $bar instanceof stdClass?'yes':'no'; //output:no
Another example:
<?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) ?>
Any cast using (object) will get an instance of stdClass.
Understanding the stdClass class in PHP
stdClass only became popular in PHP5. And stdClass is also a reserved class of zend. stdClass is a base class of PHP. Almost all classes inherit this class, so it can be new at any time and this variable can become an object. At the same time,
This base class has another special feature, that is, it has no methods. Any variable that uses new stdClass() will not be used in this way $a->test(). The uniqueness of PHP5 objects is that when the object is called anywhere, it is of reference address type, so it consumes relatively less resources. When assigning values to it on other pages, it is modified directly instead of referencing a copy.
Most of the above definitions are correct, but there is a fatal diagnostic error: stdClass is a base class of PHP, and almost all classes inherit this class. Look at a simple example:
class EmptyClass { } $object = new EmptyClass(); if ($object instanceof stdClass) { echo 'yes'; }else{ echo 'no'; }
ZEND_MINIT_FUNCTION(core) { /* {{{ */ zend_class_entry class_entry; /* 注册stdClass 类 */ INIT_CLASS_ENTRY(class_entry, "stdClass", NULL); zend_standard_class_def = zend_register_internal_class(&class_entry TSRMLS_CC); /* 注册默认类,接口,如Exception类,SPL中的一些类等 */ zend_register_default_classes(TSRMLS_C); return SUCCESS; } /* }}} */This is the module initialization function of zend_builtin_module. This function will be automatically loaded when the PHP kernel performs the module initialization operation. In this way, the registration operation of the stdClass class will also be executed. As can be seen from this code, the stdClass class is a class with no member variables and no member methods. All its magic methods, parent classes, interfaces, etc. are set to NULL during initialization. Since we cannot dynamically add methods to a class in PHP, this class can only be used to handle dynamic attributes, which is also a common usage. To summarize:
stdClass class is an internal reserved class of PHP. Initially, it has no member variables or member methods. All magic methods are set to NULL. You can use it to pass variable parameters, but there are no methods that can be called. The stdClass class can be inherited, but doing so does not make sense.
For more articles related to the new stdClass internal reserved class in PHP5, please pay attention to the PHP Chinese website!