I made a patch to the latest PHP5-CVS wich implements this hook. I think
it's useful to make a meaningful object stringfied value.

Can anyone verify it and maybe commit it to CVS ?

--- zend.c 2003-07-30 14:07:36.000000000 -0300
+++ zend.c.new 2003-08-03 00:27:43.000000000 -0300
@@ -227,10 +227,24 @@
    if (expr->value.obj.handlers->cast_object) {
     TSRMLS_FETCH();
     expr->value.obj.handlers->cast_object(expr, expr_copy, IS_STRING, 0
TSRMLS_CC);
    } else {
-    expr_copy->value.str.val = (char *) emalloc(sizeof("Object id #")-1 +
MAX_LENGTH_OF_LONG);
-    expr_copy->value.str.len = sprintf(expr_copy->value.str.val, "Object id
#%ld", (long)expr->value.obj.handle);
+      //call to_string method
+      zval *fname, *retval;
+      MAKE_STD_ZVAL(fname);
+      ZVAL_STRING(fname, "to_string", 1);
+      TSRMLS_FETCH();
+      if (call_user_function_ex(NULL, &expr, fname, &retval, 0, NULL, 0,
NULL TSRMLS_CC) == SUCCESS) {
+         if (Z_TYPE_P(retval) != IS_STRING) {
+     convert_to_string(retval);
+         }
+         ZVAL_STRINGL(expr_copy, Z_STRVAL_P(retval), Z_STRLEN_P(retval),
1);
+         zval_ptr_dtor(&retval);
+      } else {
+                                     Z_STRVAL_P(expr_copy) = (char *)
emalloc(sizeof("Object id #")-1 + MAX_LENGTH_OF_LONG);
+                                     Z_STRLEN_P(expr_copy) =
sprintf(Z_STRVAL_P(expr_copy), "Object id #%ld",
(long)Z_OBJ_HANDLE_P(expr));
+      }
+      zval_ptr_dtor(&fname);
    }
 #if 0
    /* FIXME: This might break BC for some people */
    expr_copy->value.str.len = sizeof("Object")-1;


Thanx,

Cristiano Duarte.

"Cristiano Duarte" <[EMAIL PROTECTED]> escreveu na mensagem
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
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
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
> >
> >
>
>



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

Reply via email to