Re: __toString() must return a string value
| From: | Johannes Schlüter | Date: | Sat, 21 Feb 2009 19:19:40 +0000 |
| Subject: | Re: __toString() must return a string value | ||
| References: | 1 2 3 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-6143@lists.php.net to get a copy of this message | ||
Hi,
On Sat, 2009-02-21 at 20:02 +0100, Piotr Pokora wrote:
> >> 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;
>
> return_value is predefined variable of type zval, so what it's the
> difference?
it is no zval but a pointer to a zval (zval* for short)
> Looks like it's common practice to implement code like:
>
> object_init_ex(return_value, ce);
> array_init(return_value);
>
> Isn't it?
That code is passing the pointer so these unctions can work on the zval
the pointer is pointing to.
> > [...]
> >> 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.
>
> Why? Isn't it DateTime::format responsible to set string?
It might return a NULL-zval in case it has problems passing the
parameter or in case of another error.
> Besides... RETURN_STRING will copy strv to return_value.
It will interpret the value as sring value and copy it, in case format
returns a NUL zval this might be a andom amount of random memory with a
little luck you get no weird data but the system emits a sementation
ault to tell you it's wrong.
> And, am I really responsible to set type of zval when passing it as
> in/out function argument?
Yes.
> Again. Is there common convention to (not) use return_value?
use return_value to find the zval structure, not having it point
somewhere else.
Probably you should fetch a C book and e-read the chapter about
pointers.
johnnes