Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:3703 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 59813 invoked from network); 3 Aug 2003 09:12:06 -0000 Received: from unknown (HELO hasele) (195.43.169.26) by pb1.pair.com with SMTP; 3 Aug 2003 09:12:06 -0000 Received: by hasele (Postfix, from userid 1000) id 3D4174EB68; Sun, 3 Aug 2003 02:57:40 -0400 (EDT) To: Marcus =?ISO-8859-1?Q?B=C3=B6rger?= Cc: Cristiano Duarte , internals@lists.php.net In-Reply-To: <13610291366.20030803105909@post.rwth-aachen.de> References: <20030802224959.58552.qmail@pb1.pair.com> <20030802230007.65232.qmail@pb1.pair.com> <20030803033323.97051.qmail@pb1.pair.com> <13610291366.20030803105909@post.rwth-aachen.de> Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Message-ID: <1059893859.3551.11.camel@hasele> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.3 Date: 03 Aug 2003 02:57:40 -0400 Subject: Re: [PHP-DEV] Re: hook cast_object handler in userspace From: sterling@bumblebury.com (Sterling Hughes) 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=F6rger: > Hello Cristiano, >=20 > Sunday, August 3, 2003, 5:33:25 AM, you wrote: >=20 > CD> I made a patch to the latest PHP5-CVS wich implements this hook. I th= ink > CD> it's useful to make a meaningful object stringfied value. >=20 > CD> Can anyone verify it and maybe commit it to CVS ? >=20 > 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 =3D (char *) emalloc(sizeof("Object id #")= -1 + > CD> MAX_LENGTH_OF_LONG); > - expr_copy->>value.str.len =3D sprintf(expr_copy->value.str.val, "Obj= ect 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) =3D=3D SUCCESS) { > CD> + if (Z_TYPE_P(retval) !=3D IS_STRING) { > CD> + convert_to_string(retval); > CD> + } > CD> + ZVAL_STRINGL(expr_copy, Z_STRVAL_P(retval), Z_STRLEN_P(retv= al), > CD> 1); > CD> + zval_ptr_dtor(&retval); > CD> + } else { > CD> + Z_STRVAL_P(expr_copy) =3D (char= *) > CD> emalloc(sizeof("Object id #")-1 + MAX_LENGTH_OF_LONG); > CD> + Z_STRLEN_P(expr_copy) =3D > 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 =3D sizeof("Object")-1; >=20 >=20 > CD> Thanx, >=20 > CD> Cristiano Duarte. >=20 > CD> "Cristiano Duarte" escreveu na mensagem > CD> news:20030802230007.65232.qmail@pb1.pair.com... > >> Sorry guys, > >> > >> > $o =3D new xxx(); > >> should be $o =3D 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" 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 > 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 =3D "Hello"; > >> > public function to_string() { //overhiding default to_string wic= h > >> prints > >> > "Object id #n" > >> > return $this->value; > >> > } > >> > } > >> > > >> > $o =3D new my_object(); > >> > echo $o; > >> > > >> > or: > >> > > >> > $o =3D new xxx(); > >> > echo "My stringfied value is=3D$o"; > >> > > >> > Thanx, > >> > > >> > Cristiano Duarte > >> > > >> > > >> > >> >=20 >=20 >=20 >=20 > 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. >=20 > --=20 > Best regards, > Marcus mailto:helly@php.net --=20 UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.=20 - Doug Gwyn