php-extension - php扩展中实例化对象并调用方法

WBOY
Libérer: 2016-06-06 20:49:19
original
1144 Les gens l'ont consulté

使用函数宏call_user_function调用类中的方法:
如下:

<code>ZEND_FUNCTION(call_say){
//调用myclass中的say方法
zval *obj,*method_construct,*method_say,*name,*age;
int name_len,age_len;
zend_uint param_count=ZEND_NUM_ARGS();
zval *params[param_count];

MAKE_STD_ZVAL(obj);
MAKE_STD_ZVAL(method_construct);
MAKE_STD_ZVAL(method_say);

object_init_ex(obj, people_ce);

ZVAL_STRINGL(method_construct,"__construct",strlen("__construct"),1);
ZVAL_STRINGL(method_say,"say",strlen("say"),1);

//获取参数
if(zend_parse_parameters(param_count TSRMLS_CC,"ss",&name,&name_len,&age,&age_len)==FAILURE){
    WRONG_PARAM_COUNT;
}

params[0]=name;
params[1]=age;

//__construct中存在参数 
call_user_function(NULL, &obj, method_construct, NULL,param_count,params TSRMLS_CC); 
call_user_function(NULL, &obj, method_say,NULL,0,NULL TSRMLS_CC);

zval_ptr_dtor(&obj);
zval_ptr_dtor(&method_construct);
zval_ptr_dtor(&method_say);
return;
</code>
Copier après la connexion
Copier après la connexion

}

这个call_user_function好像用错了样的。执行出现 Segmentation fault ! 哪位大神帮看下!
我想要的效果:
类中的两个方法:

<code>public function __construct($name,$age){
        $this->name=$name;
        $this->age=$age;
    }
    public function say(){
        echo "我叫{$this->name},我今年{$this->age}岁了";
    }
</code>
Copier après la connexion
Copier après la connexion

补充:自己解决了
retval没有设置就出现这个错误:
首先定义:zval *retval;
分配内存:MAKE_STD_ZVAL(retval)

<code>call_user_function(NULL, &obj, method_say, retval, 0, NULL TSRMLS_CC)
</code>
Copier après la connexion
Copier après la connexion

segmentfault的人气好像不行。。。。

回复内容:

使用函数宏call_user_function调用类中的方法:
如下:

<code>ZEND_FUNCTION(call_say){
//调用myclass中的say方法
zval *obj,*method_construct,*method_say,*name,*age;
int name_len,age_len;
zend_uint param_count=ZEND_NUM_ARGS();
zval *params[param_count];

MAKE_STD_ZVAL(obj);
MAKE_STD_ZVAL(method_construct);
MAKE_STD_ZVAL(method_say);

object_init_ex(obj, people_ce);

ZVAL_STRINGL(method_construct,"__construct",strlen("__construct"),1);
ZVAL_STRINGL(method_say,"say",strlen("say"),1);

//获取参数
if(zend_parse_parameters(param_count TSRMLS_CC,"ss",&name,&name_len,&age,&age_len)==FAILURE){
    WRONG_PARAM_COUNT;
}

params[0]=name;
params[1]=age;

//__construct中存在参数 
call_user_function(NULL, &obj, method_construct, NULL,param_count,params TSRMLS_CC); 
call_user_function(NULL, &obj, method_say,NULL,0,NULL TSRMLS_CC);

zval_ptr_dtor(&obj);
zval_ptr_dtor(&method_construct);
zval_ptr_dtor(&method_say);
return;
</code>
Copier après la connexion
Copier après la connexion

}

这个call_user_function好像用错了样的。执行出现 Segmentation fault ! 哪位大神帮看下!
我想要的效果:
类中的两个方法:

<code>public function __construct($name,$age){
        $this->name=$name;
        $this->age=$age;
    }
    public function say(){
        echo "我叫{$this->name},我今年{$this->age}岁了";
    }
</code>
Copier après la connexion
Copier après la connexion

补充:自己解决了
retval没有设置就出现这个错误:
首先定义:zval *retval;
分配内存:MAKE_STD_ZVAL(retval)

<code>call_user_function(NULL, &obj, method_say, retval, 0, NULL TSRMLS_CC)
</code>
Copier après la connexion
Copier après la connexion

segmentfault的人气好像不行。。。。

学习了,刚好用到

我比你更气人,我 retval 也没问题,现在还在查错中。。。

Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!