Getting parent constructor called in a class chain - PHP 4.3.4 C extension

php.internals

Erkka Marjakangas

22 years ago
Hello! Not sure if this is the most proper place for discussing PHP extensions, so blame me if I'm in a totally wrong place here ;-) I'm sure this is as simple as standing up, but I just didn't stumble to the right place in documentation and searching the google didn't highlight anything either :-( So I have a class creation/registering and rest working allright, with one thing missing... How do I tell PHP (4.3.4) / Zend engine to automatically call parent constructors when creating a class that has parents in an extension? Or if it cannot be done automatically, what would be the way to call the parent constructor? I've got an extension with class chain similar to this PHP script: class item {var $item_str; function test1(){}}; class stringitem extends item{var $stringitem_str; function test2(){}}; In PHP script with my extension I would create: $test=new stringitem(); And it will get all the item's methods inherited automatically(test1 here), but the item constructor won't get called thus the item_str property will be missing ;-( Registering the classes in C extension ---Code--- ... INIT_OVERLOADED_CLASS_ENTRY(ce,"item", php_item_class_functions,NULL,NULL,NULL); item_class_entry_ptr = zend_register_internal_class_ex(&ce, NULL,NULL TSRMLS_CC); INIT_OVERLOADED_CLASS_ENTRY(ce,"stringitem", php_stringitem_class_functions,NULL,NULL,NULL); stringitem_class_entry_ptr = zend_register_internal_class_ex(&ce, item_class_entry_ptr,NULL TSRMLS_CC); ... ZEND_FUNCTION(item) { add_property_string(this_ptr, "item_str", "ThisIsItemClass",1);} ZEND_FUNCTION(stringitem) { //TODO: should I call item ctor manually here, if so, how? Can't it be automated? add_property_string(this_ptr, "stringitem_str", "ThisIsStringItemClass",1); } ... ---Code--- I can always isolate the constructors to their own functions and call them manually in appropriate places to get properties of the parent class(es) initialized, but it sounds like a kludge and I' sure there's an automated way to do this since methods get inherited.... Thanks in advance! Erkka Marjakangas edeATikiDOTfi

Derick Rethans

22 years ago
On Wed, 3 Mar 2004, Erkka Marjakangas wrote:
> Hello! > > Not sure if this is the most proper place for discussing PHP > extensions, so blame me if I'm in a totally wrong place here ;-)
It is :)
> How do I tell PHP (4.3.4) / Zend engine to automatically call parent > constructors when creating a class that has parents in an extension? > > Or if it cannot be done automatically, what would be the way to call > the parent constructor?
It is not done automatically
> ZEND_FUNCTION(stringitem) > { > //TODO: should I call item ctor manually here, if so, how? Can't it be automated? > add_property_string(this_ptr, "stringitem_str", "ThisIsStringItemClass",1); > } > ... > ---Code--- > > I can always isolate the constructors to their own functions and call > them manually in appropriate places to get properties of the parent > class(es) initialized, but it sounds like a kludge and I' sure there's > an automated way to do this since methods get inherited....
There is no automated way for that, but what you can do is adding: zif_item(INTERNAL_FUNCTION_PARAM_PASSTHRU); at the locattion of "TODO". This *might* work. (Please also note that it is always smart to prefix your functions/classes with the extension name to prevent naming clashes).. Derick

Erkka Marjakangas

22 years ago
Yiihaa, it does work! Thanks a million! ----- Original Message ----- From: "Derick Rethans" <derick@php.net> To: "Erkka Marjakangas" <ede@iki.fi> Cc: <internals@lists.php.net> Sent: Wednesday, March 03, 2004 11:58 AM Subject: Re: [PHP-DEV] Getting parent constructor called in a class chain - PHP 4.3.4 C extension