简体   繁体   English

PHP扩展:如何返回一个新的类对象

[英]PHP Extension: How to return a new class object

I am creating an extension that has multiple classes and some of them have methods that should return instances of another class. 我正在创建具有多个类的扩展,其中一些具有应返回另一个类的实例的方法。 This is for PHP 7+ and not for anything else. 这适用于PHP 7+,而不适用于其他任何东西。

For example, I need the PHP code to be something like this (I'm creating an ALPM PHP extension)... 例如,我需要PHP代码像这样(我正在创建ALPM PHP扩展)...

$handle_object = new Handle("/", "/var/lib/pacman");
$db_object = $handle_object->get_localdb() /* this returns a Db object */

The code I have so far is... 到目前为止,我的代码是...

PHP_METHOD(Handle, get_localdb) {
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
        RETURN_NULL()
    }

    zval *db_obj = (zval*)emalloc(sizeof(zval));
    handle_object *intern = Z_HANDLEO_P(getThis());

    object_init_ex(db_obj, alpm_ce_db);
}

I have no clue if I'm on the right track or far from it. 我不知道是在正确的轨道上还是远离它。

For more code from the project (so far), I have posted it up on gist ( https://gist.github.com/markzz/27b0aa1123d61a5f9d80ee3aea390f20 ). 有关该项目的更多代码(到目前为止),我已将其发布在gist( https://gist.github.com/markzz/27b0aa1123d61a5f9d80ee3aea390f20 )上。

With help from the php chat (thanks guys)! 在php聊天的帮助下(谢谢大家)!

PHP_METHOD(Handle, get_localdb) {
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
        RETURN_NULL()
    }

    handle_object *intern = Z_HANDLEO_P(getThis());
    db_object *db;
    object_init_ex(return_value, alpm_ce_db);
    db = Z_DBO_P(return_value);
    db->db = alpm_get_localdb(intern->handle);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM