Home  >  Article  >  Backend Development  >  Detailed explanation of usage examples of internal reserved classes in php stdClass

Detailed explanation of usage examples of internal reserved classes in php stdClass

伊谢尔伦
伊谢尔伦Original
2017-06-30 10:20:541044browse

stdClass class is an internal reserved class of PHP. Initially, there are no member variables and no member methods. All magic methods are set to NULL and can be used. It passes variable arguments, but has no methods to call. 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.

<?php 
class foo {} 
$bar = new foo(); 
echo $bar instanceof stdClass?&#39;yes&#39;:&#39;no&#39;; 
//output:no

Another example:

<?php 
// 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, &#39;stdClass&#39;)); // false 
echo get_class($t) . "\n"; // &#39;CTest&#39; 
echo get_parent_class($t) . "\n"; // false (no parent) 
?>

Any use of (object)cast 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. Whenever you use new stdClass() variables,
it is impossible to use $a->test(). The uniqueness of PHP5's object is that when the object is called anywhere,
is a reference address type, so it consumes less resources. When assigning values ​​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:

class EmptyClass { 
} 
$object = new EmptyClass(); 
if ($object instanceof stdClass) { 
echo &#39;yes&#39;; 
}else{ 
echo &#39;no&#39;; 
}


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:

ZEND_MINIT_FUNCTION(core) { /* {{{ */ 
zend_class_entry class_entry; 
/* 注册stdClass 类 */ 
INIT_CLASS_ENTRY(class_entry, "stdClass", NULL); 
zend_standard_class_def = zend_register_internal_class(&class_entry TSRMLS_CC); 

/* 注册默认类,接口,如Exception类,SPL中的一些类等 */ 
zend_register_default_classes(TSRMLS_C); 
return SUCCESS; 
} 
/* }}} */

This is the module initialization function of zend_builtin_module. When the PHP kernel performs the module initialization operation, this function will be automatically loaded . 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, there are no member variables or member methods. All magic methods are set to NULL. You can use it to pass variable parameters, but There is no method to call. The stdClass class can be inherited, but there is no point in doing so.

The above is the detailed content of Detailed explanation of usage examples of internal reserved classes in php stdClass. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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