Home  >  Article  >  Backend Development  >  Learn more about the stdClass class from PHP source code_PHP tutorial

Learn more about the stdClass class from PHP source code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:32:16950browse

In Baidu Encyclopedia, the definition of stdClass is as follows:

Copy the code The code is as follows:

stdClass is only available in PHP5 Begins to become popular. 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(),
cannot be used in this way $a->test(). The uniqueness of PHP5 objects is that when the object is called anywhere,
is of reference address type, so it consumes relatively less resources. When assigning a value 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:

Copy the code The code is as follows:

class EmptyClass {

}

$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:

Copy code The code is as follows:

ZEND_MINIT_FUNCTION(core) { /* {{{ */
zend_class_entry class_entry;

/* 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/756340.htmlTechArticleIn Baidu Encyclopedia, the definition of stdClass is as follows: Copy the code as follows: stdClass only became popular in PHP5. And stdClass is also a reserved class of zend. stdClass is a PHP...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn