|
| 1 | +Creating an object |
| 2 | +------------------ |
| 3 | + |
| 4 | +Object can be created in the following ways: |
| 5 | + |
| 6 | +1. As a result of a function call. E.g.: |
| 7 | + |
| 8 | +$foo = create_new_foo("parameter"); |
| 9 | +$foo->run(); |
| 10 | + |
| 11 | +The function should create a new zval, create new object and get the |
| 12 | +handle for it, set handle and handler table as needed. Note that the |
| 13 | +handle is the only ID of the object, so it should be enough to |
| 14 | +identify it. |
| 15 | + |
| 16 | +2. Overriding create_object handler for class. E.g.: |
| 17 | + |
| 18 | +$foo = new Java("some.Class.here", "parameter"); |
| 19 | +$foo->run(); |
| 20 | + |
| 21 | +The create_object handler function should create a new zval, create |
| 22 | +new object and get the handle for it, set handle and handler table as |
| 23 | +needed, and also provide constructor method that would handle |
| 24 | +constructor call. The get_constructor handler table entry should be |
| 25 | +used for that. Do not rely class entry's constructor, unless you refer |
| 26 | +to it from get_constructor handler. |
| 27 | + |
| 28 | +Object maintenance |
| 29 | +------------------ |
| 30 | + |
| 31 | +The handlers add_ref and del_ref are called when a new zval referring |
| 32 | +to the object is created. This does not create a new object - both |
| 33 | +zvals still refer to the same object. |
| 34 | + |
| 35 | +clone_obj handler should create a new object, identical to an old one, |
| 36 | +but being a separate entity. |
| 37 | + |
| 38 | +delete_obj should destroy an object, all references to it become |
| 39 | +invalid. |
| 40 | + |
| 41 | +Object access - read |
| 42 | +-------------------- |
| 43 | + |
| 44 | +get_property is used to read object's property. This value is not |
| 45 | +meant to be changed. The handler returns zval * with the value. |
| 46 | + |
| 47 | +Object access - write |
| 48 | +--------------------- |
| 49 | + |
| 50 | +write_property is used to directly write object's property by |
| 51 | +name. This handler is not oftenly used by the engine itself, but may |
| 52 | +be used by some internal functions. |
| 53 | + |
| 54 | +get_property_ptr is used to obtain zval ** for future writing to |
| 55 | +it. If your object properties are stored as zval*, return real place |
| 56 | +where the property is stored. If the aren't, the best way is to create |
| 57 | +proxy object and handle it via get and set methods (see below). |
| 58 | + |
| 59 | +get and set handlers are used when engine needs to access the object |
| 60 | +as a value. E.g., in the following situation: |
| 61 | + |
| 62 | +$foo =& $obj->bar; |
| 63 | +$foo = 1; |
| 64 | + |
| 65 | +if $foo is an object (e.g., proxy object from get_property_ptr) it |
| 66 | +would be accessed using write handler. |
| 67 | + |
| 68 | +Object access - method call |
| 69 | +--------------------------- |
| 70 | + |
| 71 | +get_method handler is used to find method description by name. It |
| 72 | +should set right type, function name and parameter mask for the |
| 73 | +method. If the type is ZEND_OVERLOADED_FUNCTION, the method would be |
| 74 | +called via call_method handler, otherwise it would be called with |
| 75 | +standard Zend means. |
| 76 | + |
| 77 | +get_constructor performs the same function as get_method, but for the |
| 78 | +object constructor. |
| 79 | + |
| 80 | +call_method handler is used to perform method call. Parameters are |
| 81 | +passed like to any other Zend internal function. |
| 82 | + |
| 83 | +Object - comparison |
| 84 | +------------------- |
| 85 | + |
| 86 | +Objects can be compared via compare_objects handler. This is used with |
| 87 | +== operation, === compares objects by handles, i.e., return true if |
| 88 | +and only if it's really the same object. Note that objects from |
| 89 | +different object types (i.e., having different handlers) can not be |
| 90 | +compared. |
| 91 | + |
| 92 | +Objects - reflection |
| 93 | +-------------------- |
| 94 | + |
| 95 | +get_class_name is used to retrieve class name of the object. No other |
| 96 | +reflection functions are currently implemented. |
| 97 | + |
| 98 | +Objects - data structures and handlers |
| 99 | +--------------------------------------- |
| 100 | + |
| 101 | +The object is represented by the following structure: |
| 102 | + |
| 103 | +struct _zend_object_value { |
| 104 | + zend_object_handle handle; |
| 105 | + zend_object_handlers *handlers; |
| 106 | +}; |
| 107 | + |
| 108 | +handle is an ID of the object among the objects of the same type (not |
| 109 | +class!). The type of the object and how it behaves is determined by |
| 110 | +the handler table. |
| 111 | + |
| 112 | +typedef struct _zend_object_handlers { |
| 113 | + zend_object_add_ref_t add_ref; |
| 114 | + zend_object_del_ref_t del_ref; |
| 115 | + zend_object_delete_obj_t delete_obj; |
| 116 | + zend_object_clone_obj_t clone_obj; |
| 117 | + zend_object_read_property_t read_property; |
| 118 | + zend_object_write_property_t write_property; |
| 119 | + zend_object_get_property_ptr_t get_property_ptr; |
| 120 | + zend_object_get_t get; |
| 121 | + zend_object_set_t set; |
| 122 | + zend_object_has_property_t has_property; |
| 123 | + zend_object_unset_property_t unset_property; |
| 124 | + zend_object_get_properties_t get_properties; |
| 125 | + zend_object_get_method_t get_method; |
| 126 | + zend_object_call_method_t call_method; |
| 127 | + zend_object_get_constructor_t get_constructor; |
| 128 | + zend_object_get_class_name_t get_class_name; |
| 129 | + zend_object_compare_t compare_objects; |
| 130 | +} zend_object_handlers; |
| 131 | + |
| 132 | +See zend_object_handlers.h for prototypes. All objects are passed as zval's. |
| 133 | + |
| 134 | +Handlers explained: |
| 135 | + |
| 136 | +add_ref - called when a copy of the object handle is created. |
| 137 | + |
| 138 | +del_ref - called when a copy of the object handle is destroyed. |
| 139 | + |
| 140 | +delete_obj - called when an object needs to be destroyed. |
| 141 | + |
| 142 | +clone_obj - called when a new object identical to an old one should be |
| 143 | +created (unlike Zend Engine 1, this never happens unless explicitly |
| 144 | +asked for). |
| 145 | + |
| 146 | +read_property - returns zval *, containing the value of the |
| 147 | +property. Is used when value of the property should be retrieved for |
| 148 | +reading. |
| 149 | + |
| 150 | +write_property - assigns value to certain property of the object. |
| 151 | + |
| 152 | +get_property_ptr - retrieves zval** for the property of the value, to |
| 153 | +be used for read and write. If object properties are not zval's |
| 154 | +natively, this method should create and return proxy object for use |
| 155 | +with get and set methods. |
| 156 | + |
| 157 | +get - retrieves zval* for object contents. To be used mainly with |
| 158 | +proxy objects from get_property_ptr, but also may be used for |
| 159 | +convert_to_* functions. |
| 160 | + |
| 161 | +set - sets value for object contents. To be used mainly with |
| 162 | +proxy objects from get_property_ptr. |
| 163 | + |
| 164 | +has_property - checks if the object has certain property set. |
| 165 | + |
| 166 | +unset_property - removes value for the property of the object |
| 167 | + |
| 168 | +get_method - retrieves description of the method |
| 169 | + |
| 170 | +call_method - calls the method (parameters should be put on stack like |
| 171 | +for any other PHP internal function). |
| 172 | + |
| 173 | +get_constructor - get description for the object constructor method |
| 174 | + |
| 175 | +get_class_name - get the name of the class the object belongs to |
| 176 | + |
| 177 | +compare_objects - compares if two objects are equal |
0 commit comments