PHP5 New Stdclass Internal retention class _php tutorial

Source: Internet
Author: User
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.

This class is a reserved class for PHP, not a base class for all classes.
Copy CodeThe code is as follows:
class Foo {}
$bar = new Foo ();
echo $bar instanceof StdClass? ' Yes ': ' No ';
Output:no

Another example:
Copy CodeThe code is as follows:
CTest does not derive from StdClass
Class CTest {
Public $property 1;
}
$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 cast with (object) will get an instance of Stdclass.


Understanding the Stdclass class in PHP

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:
Copy CodeThe 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 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:
Copy CodeThe 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 the 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.

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

http://www.bkjia.com/PHPjc/323531.html www.bkjia.com true http://www.bkjia.com/PHPjc/323531.html techarticle 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 no ...

  • Related Article

    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.