Re: Re: hook cast_object handler in userspace

From: Date: Sun, 03 Aug 2003 08:59:09 +0000
Subject: Re: Re: hook cast_object handler in userspace
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-3702@lists.php.net to get a copy of this message
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" <cunha17@uol.com.br> escreveu na mensagem CD> 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 CD> would >> make all objects inherit the to_string method, it would be great ! >> >> Cristiano Duarte >> >> "Cristiano Duarte" <cunha17@uol.com.br> 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 = "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:helly@php.net

« previous php.internals (#3702) next »