php5 objects
##It’s a bit confusing to compare php5 objects with their predecessors, php4 objects. Fair enough, but the API functions used by php5 objects are still built according to the API of php4. If you have read Chapter 10 "php4 Objects", you will be somewhat familiar with the content of this chapter. Before starting this chapter, you can like Chapter 10 Same as the beginning, rename the extension to sample3 and clean up the redundant code, leaving only the skeleton code of the extension.
Evolutionary History
There are two key components in php5 object variables. The first is a numerical identifier, which is very similar to the numerical resource ID introduced in Chapter 9 "Resource Data Type", and plays a role used in The role of the key to find the object instance in the corresponding table. The elements in this instance table contain references to zend_class_entry and the internal attribute table.
The second element is the object variable A handle table that allows you to customize how the Zend Engine handles instances. You will see this handle table later in this chapter.
zend_class_entry
A class entry is an internal representation of a class you define in user space. As you saw in the previous chapter, this structure is initialized by calling INIT_CLASS_ENTRY() with the class name and its function table. Next Use zend_register_internal_class() to register in the MINIT phase.
zend_class_entry *php_sample3_sc_entry; #define PHP_SAMPLE3_SC_NAME "Sample3_SecondClass" static function_entry php_sample3_sc_functions[] = { { NULL, NULL, NULL } }; PHP_MINIT_FUNCTION(sample3) { zend_class_entry ce; INIT_CLASS_ENTRY(ce, PHP_SAMPLE3_SC_NAME, php_sample3_sc_functions); php_sample3_sc_entry = zend_register_internal_class(&ce TSRMLS_CC); return SUCCESS; }
Method
If you have read the previous chapter, You might be thinking "Looks pretty much the same so far?", and so far, you'd be right. Now let's start defining some object methods. You'll start to see some very definite and welcome differences.
PHP_METHOD(Sample3_SecondClass, helloWorld) { php_printf("Hello World\n"); }
The PHP_METHOD() macro was introduced in Zend Engine 2. It is an encapsulation of the PHP_FUNCTION() macro. It combines the class name and method name without manually defining the method like in php4. By using this macro, the namespace resolution specification of your code in the extension is consistent with that of other maintainers' code.
Definition
Define the implementation of a method, just like other functions, except that it is connected to the function table of the class. In addition to the PHP_METHOD() macro used for implementation, there are some new Macros can be used in the definition of function lists.
PHP_ME(classname, methodname, arg_info, flags)
Compared with the PHP_FE() macro introduced in Chapter 5 "Your First Extension", PHP_ME() adds a classname parameter and a flags parameter at the end (used to provide public, protected , private, static and other access controls, as well as abstract and other options). For example, to define the helloWorld method, you can define it as follows:
PHP_ME(Sample3_SecondClass,helloWorld,NULL,ZEND_ACC_PUBLIC)
PHP_MALIAS(classname, name, alias, arg_info, flags)
is very similar to the PHP_FALIAS() macro. This macro allows you to give the method described by the alias parameter (in the same class ) implementation provides a new name specified by name. For example, to copy your helloWorld method, you can define it as follows
PHP_MALIAS(Sample3_SecondClass, sayHi, helloWorld, NULL, ZEND_ACC_PUBLIC)
PHP_ABSTRACT_ME(classname, methodname, arg_info)
内部类中的抽象方法很像用户空间的抽象方法. 在父类中它只是一个占位符, 期望它的子类提供真正的实现. 你将在接口一节中使用这个宏, 接口是一种特殊的class_entry.
PHP_ME_MAPPING(methodname, functionname, arg_info)
最后一种方法定义的宏是针对同时暴露OOP和非OOP接口的扩展(比如mysqli既有过程化的mysqli_query(), 也有面向对象的MySQLite::query(), 它们都使用了相同的实现.)的. 假定你已经有了一个过程化函数, 比如第5章写的sample_hello_world(), 你就可以使用这个宏以下面的方式将它附加为一个类的方法(要注意, 映射的方法总是public, 非static, 非final的):
PHP_ME_MAPPING(hello, sample_hello_world, NULL)
现在为止, 你看到的方法定义都使用了ZEND_ACC_PUBLIC作为它的flags参数. 实际上, 这个值可以是下面两张表的任意值的位域运算组合, 并且它还可以和本章后面"特殊方法"一节中要介绍的一个特殊方法标记使用位域运算组合.
类型标记 | 含义 |
ZEND_ACC_STATIC | 方法可以静态调用.实际上,这就表示,方法如果通过实例调用, $this或者更确切的说this_ptr,并不会被设置到实例作用域中 |
ZEND_ACC_ABSTRACT | 方法并不是真正的实现.当前方法应该在被直接调用之前被子类覆写. |
ZEND_ACC_FINAL | ##Methods cannot be overridden by subclasses |
#Visibility tag | Meaning | ||||||||||||||||||||
ZEND_ACC_PUBLIC | Can be called in any scope outside the object .This is the same as the visibility of the php4 method | ||||||||||||||||||||
Method | Usage |
__construct(...) | Optional automatically called object constructor(The method previously defined is the same as the class name).If __construct() and classname()Both implementations exist,In the instantiation During the process, , will give priority to calling __construct() |
# #__destruct() | When the instance leaves the scope,or requests the entire termination , will cause the __destruct() method of the instance to be implicitly called to handle some cleanup work, For example, closing a file or network handle. |
__clone() | ##By default,All instances are true pass-by-reference.In php5, If you want to truly copy an object instance, you must useclonekeyword.When the clone keyword is called on an object instance, The __clone() method will be executed implicitly,It allows the object to copy some required internal resource data. |
##__toString() | When using text to represent an object ,For example, when using the echo or print statement directly on the object, The __toString() method will be automatically called by the engine.If the class implements this magic method,should return A string containing a description of the object's current state . |
__get($var) | If the script requests an invisible attribute of an object(does not exist or is invisible due to access control )hour, __get()The magic method will be called,The only parameter is the requested attribute name.implementation You can use its own internal logic to determine the most reasonable return value to return . |
__set($var , $value) | is very similar to __get(), __set()Provides the opposite ability,It is used to handle the logic when assigning invisible properties to objects. Implementations of __set() may choose to implicitly create these variables in standard attribute tables,set values using other storage mechanisms, Or throw an error directly and discard the value. |
__call() magic method Processing.This method accepts two parameters:The name of the called method,Array containing the numeric indices of all arguments passed when calling . | |
php5.1.0After, the call of isset($obj->prop) is not only a check# Is there prop in ##$obj? , will also call __isset()method,Dynamic evaluation try to use dynamic Whether the __get() and __set() methods can successfully read and write properties | |
## Similar to | __isset(), php 5.1 .0Introduces a simple OOP interface for the unset() function ,It can be used for object properties,Although this property may not exist in the object's standard property table, But it might make sense for the dynamic property space of __get() and __set(),Therefore, __unset() is introduced to solve this problem. ## 还有其他的魔术方法功能, 它们可以通过某些接口来使用, 比如ArrayAccess接口以及一些SPL接口. 在一个内部对象的实现中, 每个这样的"魔术方法"都可以和其他方法一样实现, 只要在对象的方法列表中正确的定义PHP_ME()以及PUBLIC访问修饰符即可.对于 __get(), __set(), __call(), __isset()以及__unset(), 它们要求传递参数, 你必须定义恰当的arg_info结构来指出方法需要一个或两个参数. 下面的代码片段展示了这些木梳函数的arg_info和它们对应的PHP_ME()条目: 要注意__construct, __destruct, __clone使用位域运算符增加了额外的常量. 这三个访问修饰符对于方法而言是特殊的, 它们不能被用于其他地方. 属性 php5中对象属性的访问控制与方法的可见性有所不同. 在标准属性表中定义一个公开属性时, 就像你通常期望的, 你可以使用zend_hash_add()或add_property_*()族函数. 对于受保护的和私有的属性, 则需要使用新的ZEND_API函数: 这个函数会分配一块新的内存, 构造一个"\0classname\0propname"格式的字符串. 如果类名是特定的类名, 比如Sample3_SecondClass, 则属性的可见性为private, 只能在Sample3_SecondClass对象实例内部可见. 如果类名指定为*, 则属性的可见性是protected, 它可以被对象实例所属类的所有祖先和后辈访问. 实际上, 属性可以以下面方式增加到对象上: 通过_ex()版的add_property_*()族函数, 可以明确标记属性名的长度. 这是需要的, 因为在protected和private属性名中会包含NULL字节, 而strlen()认为NULL字节是字符串终止标记, 这样将导致属性名被认为是空. 要注意的是_ex()版本的add_property_*()函数还要求显式的传递TSRMLS_CC. 而通常它是通过宏扩展隐式的传递的. 定义类常量和定义类属性非常相似. 两者的关键不同点在于它们的持久性, 因为属性的生命周期是伴随的实例的, 它发生在请求中, 而常量是和类定义在一起的, 只能在MINIT阶段定义. 由于标准的zval *维护宏的函数假定了非持久性, 所以你需要手动写不少代码. 考虑下面的函数: 在这之下, 这些类常量就可以访问了, 分别是: Sample3_SecondClass::E和Sample3_SecondClass::GREETING. 接口 接口的定义和类的定义除了几个差异外基本一致. 首先是所有的方法都定义为抽象的, 这可以通过PHP_ABSTRACT_ME()宏来完成. 由于这些方法是抽象的, 所以不需要实现. 接下来的第二个差异就是注册. 和一个实际的类注册类似, 首先调用INIT_CLASS_ENTRY和zend_register_internal_class. 当类(zend_class_entry)可用时, 最后一部就是标记这个类是接口, 实现方法如下: 实现接口 假设你想让Sample3_SecondClass这个类实现Sample3_Interface这个接口, 就需要实现这个接口定义的所有抽象方法: 接着在php_sample3_sc_functions列表中定义它们: 最后, 定义你新注册的类实现php_sample3_iface_entry接口: 如果Sample3_SecondClass实现了其他接口, 比如ArrayAccess, 就需要将对应的类(zend_class_entry)作为附加参数增加到zend_class_implements()调用中, 并将现在传递为数字1的参数值相应的增大为2: 句柄 ZE2并没有把所有的对象实例看做是相同的, 它为每个对象实例关联了句柄表. 当在一个对象上执行特定的操作时, 引擎调用执行对象的句柄表中自定义的行为. 标准句柄 默认情况下, 每个对象都被赋予了std_object_handlers这个内建句柄表. std_object_handlers中对应的句柄方法以及它们的行为定义如下: void add_ref(zval *object TSRMLS_DC) 当对象值的refcount增加时被调用, 比如, 当一个对象变量赋值给新的变量时. add_ref和del_ref函数的默认行为都是调整内部对象存储的refcount. void del_ref(zval *object TSRMLS_DC) 和add_ref类似, 这个方法也在修改refcount时调用, 通常是在unset()对象变量时发生的. zend_object_value clone_obj(zval *object TSRMLS_DC) 用于利用已有的对象实例创建一个新的实例. 默认行为是创建一个新的对象实例, 将它和原来的句柄表关联, 拷贝属性表, 如果该对象的类定义了__clone()方法, 则调用它让新的对象执行一些附加的复制工作. zval *read_property(zval *obj, zval *prop, int type TSRMLS_DC) void write_property(zval *obj, zval *prop, zval *value TSRMLS_DC) 在用户空间尝试以$obj->prop方式访问, 去读写对象的属性时, read_property/write_property对应的被调用. 默认的处理是首先在标准属性表中查找属性. 如果属性没有定义, 则检查是否存在__get()或__set()魔术方法, 如果有则调用该方法. zval **get_property_ptr_ptr(zval *obj, zval *value TSRMLS_DC) get_property_ptr_ptr() is a variant of read_property(), which allows the calling scope to directly replace the current zval * with a new one. The default behavior is to return the standard property table The pointer address of the attribute in . If it does not exist, and there is no __get()/__set() magic method, the pointer is implicitly created and returned. If the __get() or __set() method exists, it will cause this handle to fail , causing the engine to instead rely on separate read_property and write_property calls. ##zval *read_dimension(zval *obj, zval *idx, int type TSRMLS_DC) void write_dimension(zval *obj, zval *idx, zval *value TSRMLS_DC) read_dimension () and write_dimension() are similar to the corresponding read_property() and write_property(); but they are triggered when the object is accessed as an array using the $obj['idx'] method. If the object's class does not implement the ArrayAccess interface, the default The behavior is to trigger an error; otherwise it calls the magic methods offsetget($idx) or offsetset($idx, $value). ##int has_dimension(zval *obj, zval *idx, int chk_type TSRMLS_DC) When calling isset() when treating an object as an array (such as isset($obj['idx'])), this processor is used. The default standard processor will check whether the object is implemented If the ArrayAccess interface is implemented, the offsetexists($idx) method is called. If not found (referring to calling offsetexists()), it is the same as if the offsetexists() method is not implemented, and 0 is returned. Otherwise, if chk_type is 0, return directly true(1). A chk_type of 1 indicates that it must call the offsetget($idx) method of the object and test the return value,
Check whether the value is FALSE before returning TRUE(1). void unset_property(zval *obj, zval *prop TSRMLS_DC) These two methods when trying to unload object attributes ( or when applying unset() to an object in an array). The unset_property() handler either removes the property from the standard property table (if it exists), or attempts to call the implemented __unset($prop) method (php 5.1. 0), unset_dimension() calls the offsetunset($idx) method when the class implements ArrayAccess. ##HashTable *get_properties(zval *obj TSRMLS_DC) This processor is actually called when the internal function uses the Z_OBJPROP() macro to read properties from the standard property table. The default processing of PHP objects The processor unpacks and returns Z_OBJ_P(object)->properties, which is the real standard property table. ##union _zend_function *get_method(zval **obj_ptr char *method_name, int methodname_len TSRMLS_DC) This handler is called when parsing an object method in the class's function_table. If the method does not exist in the main function_table, the default handler returns a pointer to the object. _call($name, $args) method wrapped zend_function * pointer. ##int call_method(char *method, INTERNAL_FUNCTION_PARAMETERS) Function defined as ZEND_OVERLOADED_FUNCTION type will be executed as call_method processor. By default, this processor is undefined. union _zend_function *get_constructor(zval *obj TSRMLS_DC) Similar to the get_method() processor, this processor returns a corresponding object method Reference. The constructor in the zend_class_entry of the class is stored in a special way, which makes it special. Overriding of this method is very rare. zend_class_entry * get_class_entry(zval *obj TSRMLS_DC) Similar to get_constructor(), this processor is rarely overridden. Its purpose is to map an object instance Return to its original class definition. Translation Note: The above handle table and The php-5.4.9 used by the translator is no longer completely consistent. When studying this part, readers can refer to the standard processor handle table at the bottom in Zend/zend_object_handlers.c. Magic Method Part 2 使用前面看到的对象句柄表的自定义版本, 可以让内部类提供与在用户空间基于对象或类的__xxx()魔术方法相比, 相同或更多的能力.将这些自定义的句柄设置到对象实例上首先要求创建一个新的句柄表. 因为你通常不会覆写所有的句柄, 因此首先将标准句柄表拷贝到你的自定义句柄表中再去覆写你想要修改的句柄就很有意义了: 要将这个句柄表应用到对象上, 你有两种选择. 最简单也是最具代表性的就是实现一个构造器方法, 并在其中重新赋值变量的句柄表. 当构造器返回时, 对象就有了新的句柄表以及对应的自定义行为. 还有一种更加受欢迎的方法是覆写类的对象创建函数. 这样就可以在MINIT阶段注册类(zend_class_entry)之后直接将自定义句柄表附加上去. 这两种方法唯一可预见的不同是它们发生的时机不同. 引擎在碰到new Sample3_SecondClass后会在处理构造器及它的参数之前调用create_object. 通常, 你计划覆盖的各个点使用的方法(create_object Vs. __construct)应该一致. 译注: php-5.4.9中, xxx_property/xxx_dimension这一组句柄的原型是不一致的, 因此, 按照原著中的示例, 直接将xxx_property/xxx_dimension进行映射已经不能工作, 要完成上面的功能, 需要对4个句柄均包装一个函数去映射. 由于译者没有详细跟踪具体在哪一个版本发生了这些改变, 因此这里不给出译者测试的示例(没有做兼容性处理检查), 如果读者碰到这个问题, 请检查自己所使用php版本中两组句柄原型的差异并进行相应修正. 小结 毋庸置疑, php5/ZE2的对象模型比它的前辈php4/ZE1中的对象模型更加复杂. 在看完本章中介绍的所有特性和实现细节后, 你可能已经被它的所包含的信息量搞得手足无措. 幸运的是, php中在OOP之上有一层可以让你选择你的任务所需的部分而不关心其他部分. 找到复杂性之上一个舒适的层级开始工作, 剩下的都会顺起来的. 现在已经看完了所有的php内部数据类型, 是时候回到之前的主题了: 请求生命周期. 接下来的两章, 将在你的扩展中使用线程安全全局变量增加内部状态, 定义自定义的ini设置, 定义常量, 以及向使用你扩展的用户空间脚本提供超级全局变量. 以上就是[翻译][php扩展开发和嵌入式]第11章-php5对象的内容,更多相关内容请关注PHP中文网(www.php.cn)! |