Question

I am now trying to create a custom php extension and a problem I am facing is I dont know how to call a parent method.

It is like to be in the class constructor and call the parent's constructor:

zval *object,
     fname;

object = getThis();
ZVAL_STRING(&fname, "parent::__construct", 0);

if(SUCCESS != call_user_function_ex(NULL, &object, &fname, NULL, 0, NULL, 1, NULL TSRMLS_CC))
{
    RETURN_NULL();
}

How to do this correctly?

Was it helpful?

Solution

You shouldn't call the parent's constructor explicitly if all it does is initialize some of the objects properties. If you do, you'll end up with nasty things like wrong reference counts and memleaks. For an example, look at how ASTTree extends ASTNodeList.

The key part there are the zend_object_handlers, for instance create_object_tree. There's where you should do those things.

If you want to see how to call other object's methods, start following the links from the convenience macros in meta_parser.h. obj_call_method_internal_ex() is really reusable. (I've put lots of effort into making them that way)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top