Home > Backend Development > PHP Tutorial > New stdClass internal reserved class in PHP5_PHP tutorial

New stdClass internal reserved class in PHP5_PHP tutorial

WBOY
Release: 2016-07-21 15:28:58
Original
967 people have browsed it

The stdClass class is an internal reserved class of PHP. Initially, it has no member variables or member methods. All magic methods are set to NULL. You can use it to pass variable parameters, but there are no methods that can be called. The stdClass class can be inherited, but there is little point in doing so.

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

Copy code The code is as follows:

class foo {}
$bar = new foo();
echo $bar instanceof stdClass?'yes':'no';
//output: no

Another example:
Copy code 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 cast using (object) will result in an instance of stdClass.


Understanding the stdClass class in PHP

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(),
cannot be used in this way $a->test(). The uniqueness of PHP5 objects is that when the object is called anywhere,
is of reference address type, so it consumes relatively less resources. When assigning a value 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:
Copy the code The 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 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:
Copy code The 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 class, interface , 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. This function will be automatically loaded when the PHP kernel performs the module initialization operation. 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, it has no member variables or member methods. All magic methods are set to NULL, and you can use it to pass variable parameters, but There is no method to call. The stdClass class can be inherited, but there is little point in doing so.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323531.htmlTechArticlestdClass class is an internal reserved class of PHP. Initially, it has no member variables or member methods, and all magic methods are all set to NULL, which can be used to pass variable parameters, but there is no...
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