Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:3699 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 97064 invoked by uid 1007); 3 Aug 2003 03:33:23 -0000 Message-ID: <20030803033323.97051.qmail@pb1.pair.com> To: internals@lists.php.net References: <20030802224959.58552.qmail@pb1.pair.com> <20030802230007.65232.qmail@pb1.pair.com> Date: Sun, 3 Aug 2003 00:33:25 -0300 Lines: 96 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Posted-By: 200.196.104.198 Subject: Re: hook cast_object handler in userspace From: cunha17@uol.com.br ("Cristiano Duarte") 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" escreveu na mensagem news:20030802230007.65232.qmail@pb1.pair.com... > 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" escreveu na mensagem > news:20030802224959.58552.qmail@pb1.pair.com... > > 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 > > > > > >