So, being the author of the C-level cast_object(), I thought about
exposing this at one time.  I decided against it mainly because I needed
it for simplexml, and I didn't want an internals decision to be clouded
by userspace discussions (how's my whitespace btw?)

I really like the idea of PHP having an object interface that is
completely transparent.  It would be really nice if the language could
(preferably via interfaces), provide a way for userspace people to
design classes and objects that behave like base types.

Is this a bit magical?  Yes.
But its also the only reason I'd ever use objects.

If I need something to map to an external datasource, or an abstract
data type, objects are the most powerful container.  They allow for more
efficiency (fewer function calls), and a more elegant userspace
interface.   I think it would be really great if the PHP object model
contained all the necessary hooks so that something like simplexml could
be implemented in userspace.

-Sterling

Am Son, 2003-08-03 um 04.59 schrieb Marcus Börger:
> Hello Cristiano,
> 
> Sunday, August 3, 2003, 5:33:25 AM, you wrote:
> 
> CD> I made a patch to the latest PHP5-CVS wich implements this hook. I think
> CD> it's useful to make a meaningful object stringfied value.
> 
> CD> Can anyone verify it and maybe commit it to CVS ?
> 
> CD> --- zend.c 2003-07-30 14:07:36.000000000 -0300
> CD> +++ zend.c.new 2003-08-03 00:27:43.000000000 -0300
> CD> @@ -227,10 +227,24 @@
> CD>     if (expr->value.obj.handlers->cast_object) {
> CD>      TSRMLS_FETCH();
> CD>      expr->value.obj.handlers->cast_object(expr, expr_copy, IS_STRING, 0
> CD> TSRMLS_CC);
> CD>     } else {
> -    expr_copy->>value.str.val = (char *) emalloc(sizeof("Object id #")-1 +
> CD> MAX_LENGTH_OF_LONG);
> -    expr_copy->>value.str.len = sprintf(expr_copy->value.str.val, "Object id
> #%ld", (long)expr->>value.obj.handle);
> CD> +      //call to_string method
> CD> +      zval *fname, *retval;
> CD> +      MAKE_STD_ZVAL(fname);
> CD> +      ZVAL_STRING(fname, "to_string", 1);
> CD> +      TSRMLS_FETCH();
> CD> +      if (call_user_function_ex(NULL, &expr, fname, &retval, 0, NULL, 0,
> CD> NULL TSRMLS_CC) == SUCCESS) {
> CD> +         if (Z_TYPE_P(retval) != IS_STRING) {
> CD> +     convert_to_string(retval);
> CD> +         }
> CD> +         ZVAL_STRINGL(expr_copy, Z_STRVAL_P(retval), Z_STRLEN_P(retval),
> CD> 1);
> CD> +         zval_ptr_dtor(&retval);
> CD> +      } else {
> CD> +                                     Z_STRVAL_P(expr_copy) = (char *)
> CD> emalloc(sizeof("Object id #")-1 + MAX_LENGTH_OF_LONG);
> CD> +                                     Z_STRLEN_P(expr_copy) =
> CD> sprintf(Z_STRVAL_P(expr_copy), "Object id #%ld",
> CD> (long)Z_OBJ_HANDLE_P(expr));
> CD> +      }
> CD> +      zval_ptr_dtor(&fname);
> CD>     }
> CD>  #if 0
> CD>     /* FIXME: This might break BC for some people */
> CD>     expr_copy->value.str.len = sizeof("Object")-1;
> 
> 
> CD> Thanx,
> 
> CD> Cristiano Duarte.
> 
> CD> "Cristiano Duarte" <[EMAIL PROTECTED]> escreveu na mensagem
> CD> news:[EMAIL PROTECTED]
> >> Sorry guys,
> >>
> >> > $o = new xxx();
> >> should be $o = new my_object();
> >>
> >> And if there is a way to hook into zend_standard_class (stdClass) what
> CD> would
> >> make all objects inherit the to_string method, it would be great !
> >>
> >> Cristiano Duarte
> >>
> >> "Cristiano Duarte" <[EMAIL PROTECTED]> escreveu na mensagem
> >> news:[EMAIL PROTECTED]
> >> > Hi internals,
> >> >
> >> > Is there a way to hook the cast_object handler in userspace ? What I
> CD> want
> >> is
> >> > a default "to_string" method called when I do something like:
> >> >
> >> > class my_object extends class_with_default_to_string_method {
> >> >     private $value = "Hello";
> >> >     public function to_string() { //overhiding default to_string wich
> >> prints
> >> > "Object id #n"
> >> >         return $this->value;
> >> >     }
> >> > }
> >> >
> >> > $o = new my_object();
> >> > echo $o;
> >> >
> >> > or:
> >> >
> >> > $o = new xxx();
> >> > echo "My stringfied value is=$o";
> >> >
> >> > Thanx,
> >> >
> >> > Cristiano Duarte
> >> >
> >> >
> >>
> >>
> 
> 
> 
> 
> The patch doesn't compile (always use confugure --enable-maintainer-zts).
> Then you assume that every class has a function to_string(), this is not what
> we want.
> 
> -- 
> Best regards,
>  Marcus                            mailto:[EMAIL PROTECTED]
-- 
UNIX was not designed to stop you from doing stupid things, because that
would also stop you from doing clever things. 
  - Doug Gwyn

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to