Re: __toString() must return a string value
| From: | Johannes Schlüter | Date: | Sat, 21 Feb 2009 13:48:12 +0000 |
| Subject: | Re: __toString() must return a string value | ||
| References: | 1 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-6141@lists.php.net to get a copy of this message | ||
On Sat, 2009-02-21 at 13:15 +0100, Piotr Pokora wrote:
> Hi!
>
> Method implementation looks like this (for DateTime derived):
>
> static PHP_METHOD(midgard_datetime, __toString)
> {
> if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE)
> return;
>
> /* Prepare DateTime::format argument */
> zval *fmt;
> MAKE_STD_ZVAL(fmt);
> ZVAL_STRING(fmt, "c", 0);
>
> /* Invoke Datetime::format */
> zval *zend_object = getThis();
>
> zend_call_method_with_1_params(&zend_object,
> Z_OBJCE_P(zend_object), NULL, "format", &return_value, fmt);
here you are passing the address of your local variable "return_value"
so zend_call_method will change the pointer. It's basically like doing
return_value = 23;
This change has no effect on the caller of your function it will still
see a NULL-zval. You might try using return_value_ptr, which is part of
the PHP_FUNCTION signature - I never used that though :-)
[...]
> zval *strv = NULL;
> zend_call_method_with_1_params(&zend_object, Z_OBJCE_P(zend_object),
> NULL, "format", &strv, fmt);
> RETURN_STRING(Z_STRVAL_P(strv), 0);
When doing this you should check they type. You might also use
RETURN_ZVAL() if you don't care for the type.
johannes