Added stdClass internal reserved class in PHP5 _ PHP Tutorial

Source: Internet
Author: User
Added stdClass internal reserved class in PHP5. The stdClass class is an internal reserved class of PHP. it has no member variables or member methods at the beginning, and all magic methods are set to NULL. you can use it to pass variable parameters, however, no stdClass class is an internal reserved class of PHP. at the beginning, no member variables or member methods are available. all magic methods are set to NULL and can be used to pass variable parameters, however, there are no methods that can be called. The stdClass class can be inherited, but it makes no sense to do so.

This class is a reserved PHP class, not the base class of all classes.

The code is as follows:


Class foo {}
$ Bar = new foo ();
Echo $ bar instanceof stdClass? 'Yes': 'no ';
// Output: no


Another example:

The code is as follows:


// CTest does not derive from stdClass
Class CTest {
Public $ property1;
}
$ T = new CTest;
Var_dump ($ t instanceof stdClass); // false
Var_dump (is_subclass_of ($ t, 'stdclass'); // false
Echo get_class ($ t). "\ n"; // 'ctest'
Echo get_parent_class ($ t). "\ n"; // false (no parent)
?>


Any forced conversion with (object) will get an instance of stdClass.


Understanding stdClass in PHP

StdClass became popular in PHP5. 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 be made an object. At the same time,
This base class has another special feature, that is, there is no method. All variables that use new stdClass,
It is impossible to use $ a-> test. The uniqueness of PHP5 objects. objects are called anywhere,
All are address-based, so the resource consumption is relatively less. When assigning values to other pages, it is directly modified instead of referencing a copy.

Most of the above definitions are correct, but a fatal diagnostic error: stdClass is a base class of PHP, and almost all classes inherit this class. Let's look at a simple example:

The code is as follows:


Class EmptyClass {

}
$ Object = new EmptyClass ();
If ($ object instanceof stdClass ){
Echo 'yes ';
} Else {
Echo 'no ';
}


Run the code and output "no". This example fully demonstrates that the stdClass 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 take a look at the implementation of the stdClass from the source code dimension. its registration location is in the Zend/zend_buildin_functions.c file. As follows:

The code is as follows:


ZEND_MINIT_FUNCTION (core ){/*{{{*/
Zend_class_entry class_entry;
/* Register the stdClass */
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 and some classes in SPL */
Zend_register_default_classes (TSRMLS_C );
Return SUCCESS;
}
/*}}}*/


This is the module initialization function of zend_builtin_module. This function is automatically loaded when the PHP kernel initializes the module. in this way, the registration operation of the stdClass class will be executed. From this code, we can see that the stdClass class is a class without any member variables or member methods. All its magic methods, including parent classes and interfaces, are set to NULL during initialization. Since we cannot dynamically add methods to a class in PHP, this class can only be used to process dynamic attributes, which is also a common usage.

Summary:
The stdClass class is an internal reserved class of PHP. it has no member variables or member methods at the beginning, and all magic methods are set to NULL. you can use it to pass variable parameters, however, there are no methods that can be called. The stdClass class can be inherited, but it makes no sense to do so.

Official reference: http://www.php.net/manual/en/language.oop5.basic.php#92123

...

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.