PHP Instance Analysis Stdclass class

Source: Internet
Author: User
This article mainly introduces the source from the analysis of PHP in-depth understanding of the Stdclass class, and summarizes the Stdclass class is exactly what, the need for friends can refer to the next

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

Stdclass began to be popular in PHP5. And Stdclass is also a reserved class for Zend. Stdclass is a base class for PHP,

Almost all classes inherit this class, so any time you can be new, you can make the variable an object. While
There is a special place in this base class, there is no method. Any variable with the new StdClass (),
It is impossible to use $a->test () in this way. The uniqueness of the PHP5 object, where the object is called anywhere,
are referred to as the address type, so the relative consumption of resources will be less. When the other page assigns a value to it, it is modified directly instead of referencing a copy.

Most of the above definitions are correct, but a fatal diagnostic error: Stdclass is a base class for PHP, and almost all classes inherit this class. See a simple example:

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 for all classes. It's just a reserved class for PHP, or a role like the strlen function. We look at the implementation of the Stdclass class from the dimension of the source code, where it is registered in the ZEND/ZEND_BUILDIN_FUNCTIONS.C file. 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, which automatically loads this function when the PHP kernel initializes the module, so that the registration of the Stdclass class is executed. As you can see from this code, the Stdclass class is a class that has no member variables and no member methods. All of its magic methods, parent classes, interfaces, etc. are set to null at initialization time. Since we cannot dynamically add methods to a class in PHP, this class can only be used to handle dynamic properties, which is a common usage.

To summarize:

The Stdclass class is an internal reserved class for PHP that initially has no member variables and no member methods, all magic methods are set to null and can be used to pass variable arguments, but there is no method that can be called. The Stdclass class can be inherited, but it doesn't make sense to do so.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.