PHP stdClass类 使用
总结一下:
stdClass类是PHP的一个内部保留类,初始时没有成员变量也没成员方法,所有的魔术方法都被设置为NULL,可以使用其传递变量参数,但是没有可以调用的方法。stdClass类可以被继承,只是这样做没有什么意义。
这是zend_builtin_module的模块初始化函数,在PHP内核进行模块初始化操作时会自动加载这个函数, 这样,stdClass类的注册操作也就会被执行了。从这段代码可以看出,stdClass类是一个没有成员变量也没有成员方法的类。 它的所有的魔术方法,父类、接口等在初始化时都被设置成NULL。由于在PHP中对于一个类我们无法动态的添加方法, 所以这个类只能用来处理动态属性,这也是我们一种常见的用法。
执行代码,输出”no”,这个例子充分说明了stdClass类并不是所有类的基类。它仅仅 是PHP的一个保留类,或者说一个类似于strlen函数这样的一个角色。 我们从源码的维度看看stdClass类的实现,它注册的位置在 Zend/zend_buildin_functions.c文件中。如下:
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;
}
/* }}} */
或者,我们可以又这么理解一下,正因为PHP5的对象的独特性,对象在任何地方被调用,都是引用地址型的,所以相对消耗的资源会少一点。在其它页面为它赋值时是直接修改,而不是引用一个拷贝。
例如:
$user = new stdClass();
$user->name = ‘gouki’;
$myUser = $user;
$myUser->name = ‘flypig’;
如果在PHP4时代,这样的代码就是在消耗系统资源。因为:
$myUser = $user;
这是创建了一个拷贝。所以,在PHP4的时候,都是这样使用:
$myUser = & $user;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.
