PHP instance analysis stdClass class

伊谢尔伦
Release: 2023-03-12 07:16:02
Original
985 people have browsed it

This article mainly introduces an in-depth understanding of the stdClass class from the perspective of analyzing PHP source code, and summarizes what the stdClass class is. Friends in need can refer to it

In Baidu Encyclopedia, for stdClass The definition is as follows:

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(),
is unlikely to be used in this way $a->test(). The uniqueness of PHP5's object is that when the object is called anywhere,
is a reference address type, so it consumes relatively fewer 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';
}
Copy after login

executes the code and outputs "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:

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;
}
/* }}} */
Copy after login

This is the module initialization function of zend_builtin_module. When the PHP kernel performs the module initialization operation, this function will be automatically loaded . 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, 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 is no method that can be called. The stdClass class can be inherited, but there is no point in doing so.

The above is the detailed content of PHP instance analysis stdClass class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!