In Baidu Encyclopedia, the definition of stdClass is as follows:
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:
}
$object = new EmptyClass();
if ($object instanceof stdClass) {
echo 'yes';
}else{
echo 'no';
}
Execute the code and output "no". This example fully illustrates that the stdClass class is not the base class of all classes. It is just a reserved class of PHP, or a role similar to the strlen function. Let's look at the implementation of the stdClass class from the source code perspective. Its registration location is in the Zend/zend_buildin_functions.c file. As follows:
/* Register stdClass class */
INIT_CLASS_ENTRY(class_entry, "stdClass", NULL);
zend_standard_class_def = zend_register_internal_class(&class_entry TSRMLS_CC);
/* Register default classes, interfaces, such as Exception class, some classes in SPL, etc. */
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:
The 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 and can be used. It passes variable arguments, but has no methods to call. The stdClass class can be inherited, but there is little point in doing so.