OBJECTS2_HOWTO 7.1 KB

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