Learn more about Stdclass class _php skills from the source code of PHP

Source: Internet
Author: User
Tags zend

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

Copy Code code as follows:

Stdclass began to be popular in PHP5. And Stdclass is also a reserved class of Zend. Stdclass is a base class for PHP,
Almost all classes inherit this class, so any time you can be new, you can make this variable an object. While
This base class has a special place, that is, there is no method. Any variable that uses the new StdClass (),
There is no way that $a->test () can be used. The uniqueness of the object of the PHP5, where the object is invoked anywhere,
are referred to as the address type, so the relative consumption of resources will be less. When other pages assign values to it, they are directly modified, not 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. Look at a simple example:

Copy Code code as follows:

Class Emptyclass {

}

$object = new Emptyclass ();
if ($object instanceof StdClass) {
echo ' yes ';
}else{
Echo ' no ';
}

Executes the code, outputting "No", which fully demonstrates that the Stdclass class is not a 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 source dimension, where it is registered in the ZEND/ZEND_BUILDIN_FUNCTIONS.C file. As follows:

Copy Code code as follows:

Zend_minit_function (CORE) {/* {* * *
Zend_class_entry Class_entry;

/* Registered Stdclass class * *
Init_class_entry (Class_entry, "StdClass", NULL);
Zend_standard_class_def = Zend_register_internal_class (&class_entry tsrmls_cc);

/* Register the default class, interface, such as Exception class, SPL in some of the classes and so on * *
Zend_register_default_classes (Tsrmls_c);

return SUCCESS;
}
/* }}} */

This is the Zend_builtin_module module initialization function that is automatically loaded when the PHP kernel performs module initialization, 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, and so on are set to null when initialized. Because we cannot add methods dynamically to a class in PHP, this class can only be used to handle dynamic properties, which is a common usage.

To sum up:

The Stdclass class is an internal retention class for PHP, with no member variables at the beginning 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 invoked. The Stdclass class can be inherited, but there is no point in doing 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.