Hi,
If I understand you correctly you can put this pointer inside of a resource container and return the resource container to user space as the return value from url_init(). Then, from url_set_string() you accept the resource as a parameter and extract the pointer to your CPP instance of whatever class.
Tanks for your response, but your solution do not work for me, because I don’t get the resource_id as parameter for calls to the object. ($php_url_object->set_string(“asdf”); )
I now save the C++ Object pointer in a resource and save this resource as an attribute of the PHP object.
PHP_FUNCTION(url_init) { Url_class * url = new Url_class; zval * this_p = getThis(); MAKE_STD_ZVAL(return_value); int res_id = ZEND_REGISTER_RESOURCE(NULL, url, le_url); add_property_resource(getThis(), "url_class_ressource", res_id); }
It’s a little bit tricky to extract this resource and the inherit pointer again, like I do in my set_string function:
PHP_FUNCTION(url_set_string)
{
zval ** ressource;
if( zend_hash_find(getThis()->value.obj.properties,
"url_class_ressource", sizeof("url_class_ressource"),
(void**) &ressource) == FAILURE )
{
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Could not find url_class_ressource");
RETURN_FALSE;
}
if( ressource == NULL || (*ressource) == NULL ||(*ressource)->type != IS_RESOURCE ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Ressource not found"); RETURN_FALSE; }
Url_class * url; url = (Url_class *) zend_fetch_resource(ressource TSRMLS_CC, -1, "url", NULL, 1, le_url); ZEND_VERIFY_RESOURCE(url);
if( url == NULL ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Ressource is invalid???"); RETURN_FALSE; }
if(url->setString("test")) { RETURN_TRUE; }
RETURN_FALSE; }
But I don’t get memory leeks, because php makes sure my resource is destructed if it is not referenced anymore:
static ZEND_RSRC_DTOR_FUNC(destroy_url)
{
Url_class * url = (Url_class *) rsrc->ptr;;
delete url;
}
I am not to happy with this solution, because the user can access the "url_class_ressource" attribute of the PHP url Object, but this will work for now.
Or can someone give me better solution? I am happy for everything which may help me,…
Greets Jan
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php