how to create a php extension for an object?
I am working on a php extension for c++ classes. How to create a link to a method that accepts as parameter an objec开发者_JAVA百科t of a class?
Can you give me some examples? THX. APPRECIATE!
I succedded to create a link to a method that accepts as parameter a string or int. But I don't know how to do this for a method.
Here is a short example:
PHP_METHOD(Class1, method_string)
{
Class1 *access;
char *strr=NULL;
int strr_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &strr, &strr_len) == FAILURE) {
RETURN_NULL();
}
access_object *obj = (access_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
access = obj->access;
if (access != NULL) {
std::string s(strr);
RETURN_BOOL(access->method_string(s));
}
}
Use the zend API zend_parse_method_parameters()
:
ZEND_METHOD(ext_access_class, do_something)
{
zval* objid_this = NULL, objid1 = NULL;
// note: ext_access_class_entry and ext_param_class_entry are of type zend_class_entry*
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &objid_this, ext_access_class_entry, &objid1, ext_param_class_entry) == FAILURE)
RETURN_NULL();
ext_access_class* const access_obj = (ext_access_class*) zend_object_store_get_object(objid_this TSRMLS_CC);
Class1* const access = access_obj->access;
ext_param_class* const param_obj = (ext_param_class*) zend_object_store_get_object(objid1 TSRMLS_CC);
Class2* const myobject = param_obj->myobject;
const bool ret = access->do_something(myobject);
RETURN_BOOL(ret);
}
I believe ZEND_API int zend_parse_method_parameters(int num_args TSRMLS_DC, zval *this_ptr, char *type_spec, ...);
AND
ZEND_API int zend_parse_method_parameters_ex(int flags, int num_args TSRMLS_DC, zval *this_ptr, char *type_spec, ...);
are the right API for retrieving the input parameters in the method.
I think the same API will help you accept an object as an input parameter.
精彩评论