2

In php, it is valid to write something like this:

<?php
class Foo
{
    public function bar()
    {
        return $this;
    }
}
?>

How can I do this inside zend engine? I want a method to execute some operations, then return the class instance reference.

Furthermore, I would like to store some objects (from other classes) and return them as result of some other methods, should i store it's zval*? What is the right way to return a reference to it?

Wanderson Silva
  • 1,179
  • 1
  • 17
  • 35

1 Answers1

1

That's right, you need to return the zval*. You need to use RETURN_ZVAL for this, which is declared as:

RETURN_ZVAL(zv, ctor, dtor)

The first argument, zv is your zval*. The second ctor tells the Zend Engine to use the copy constructor (it's for maintaining the refcount). The last argument, dtor tells the Zend Engine to apply the destructor to zv (related to the refcount too). Typically, unless you know what you are doing, these last two arguments should be 1 and 0 respectively.

To return $this, per example:

PHP_METHOD(Foo, bar)
{
     RETURN_ZVAL(getThis(), 1, 0);
}

Here, getThis() returns a zval* to $this. You can pass any other zval* containing a PHP object if you want to.

netcoder
  • 66,435
  • 19
  • 125
  • 142