Home > php教程 > PHP开发 > body text

Learn more about the stdClass class from PHP source code

高洛峰
Release: 2016-12-21 14:19:47
Original
1177 people have browsed it

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

stdClass在PHP5才开始被流行。而stdClass也是zend的一个保留类。stdClass是PHP的一个基类,
所有的类几乎都继承这个类,所以任何时候都可以被new,可以让这个变量成为一个object。同时,
这个基类又有一个特殊的地方,就是没有方法。凡是用new stdClass()的变量,
都不可能会出现$a->test()这种方式的使用。PHP5的对象的独特性,对象在任何地方被调用,
都是引用地址型的,所以相对消耗的资源会少一点。在其它页面为它赋值时是直接修改,而不是引用一个拷贝。
Copy after login

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 'yes';
}else{
    echo 'no';
}
Copy after login

executes the code and outputs "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;
}
/* }}} */
Copy after login

To summarize:

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. You can use it to pass variable parameters, but there is no The method to call. The stdClass class can be inherited, but doing so does not make sense.

For more in-depth understanding of stdClass class related articles from PHP source code, please pay attention to the PHP Chinese website!


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!