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?

4

1 回答 1

1

没错,你需要退回zval*. 您需要使用RETURN_ZVAL它,它被声明为:

RETURN_ZVAL(zv, ctor, dtor)

第一个论点,zv是你的zval*. 第二个ctor告诉 Zend 引擎使用复制构造函数(它用于维护refcount. 最后一个参数dtor告诉 Zend 引擎将析构函数应用于zv(与refcount太相关)。通常,除非您知道自己在做什么,否则最后两个参数应该分别是10

要返回$this,例如:

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

在这里,getThis()返回一个zval*to $thiszval*如果您愿意,您可以传递任何其他包含 PHP 对象的对象。

于 2012-08-16T16:03:52.713 回答