Hi there,
I'm trying to create an object-oriented PHP 5 extension. As I don't know
any step-by-step tutorial which would explain everything in detail I'm
trying to understand various sources and documents I happen to run into.
I'd appreciate if anyone could comment on what I currently think the
typical steps look like (the more comments the better as it's a rather
random learning process):
* Before a class can be used it must be registered with
zend_register_internal_class(). The returned pointer is typically saved in
a global zend_class_entry* (why I don't know - do you need the pointer
somewhere later?).
* As you need to register a class before you can use it you must call the
code somewhere - a good place is the module startup function.
* In order to call zend_register_internal_class() you need another another
zend_class_entry first. It can be initialized with INIT_CLASS_ENTRY which
sets the class name and assigns the function table (which contains member
functions declared with PHP_ME).
* Classes are instantiated by calling the function assigned to
zend_class_entry::create_object. Thus apart from the initializiation with
INIT_CLASS_ENTRY zend_class_entry::create_object must be set in the module
startup function.
* In the create_object function a zend_object is created and initialized
(with zend_object_std_init). Furthermore the default_properties of the
class are copied to the properties of the zend_object (with
zend_hash_copy). Default properties need to be added to zend_class_entry*
after the class is registered in the module startup function (with one of
the many add_property_... functions).
* As the create_object function does not return the newly created
zend_object directly it must be stored in a zend_object_handle together
with function pointers to a destructor, free storage function and storage
clone function (is this a copy constructor if you use C++ terminology?).
The function zend_objects_store_put() can be used to do all of this.
* The zend_object_handle returned by the create_object function needs also
a pointer to zend_object_handlers. This is a hash table which contains
various object handlers - among others functions to read/write properties.
The object handlers object is typically created and initialized in the
module startup function (as it is the same for all objects of the same
class). Is there any overview what these object handlers are used for and
when exactly you should define your own? I especially wonder what the
read/write property handlers are good for as you define properties in the
class? They don't seem to be unimportant though as I've seen dozens of
them in the DOM extension for example.
Boris
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php