cvs: ZendEngine2 / zend_object_handlers.c zend_object_handlers.h zend_operators.c php-src/tests/classes tostring.phpt

From: Date: Thu, 18 Sep 2003 11:38:33 +0000
Subject: cvs: ZendEngine2 / zend_object_handlers.c zend_object_handlers.h zend_operators.c php-src/tests/classes tostring.phpt
Groups: php.zend-engine.cvs 
Request: Send a blank email to php-zend-engine-cvs+get-1846@lists.php.net to get a copy of this message
helly Thu Sep 18 07:38:33 2003 EDT Added files: /php-src/tests/classes tostring.phpt Modified files: /ZendEngine2 zend_object_handlers.c zend_object_handlers.h zend_operators.c Log: - Allow partial type conversion support for objects. - Add support for object to string conversion from userspace by method __toString() and add a test. Index: ZendEngine2/zend_object_handlers.c diff -u ZendEngine2/zend_object_handlers.c:1.68 ZendEngine2/zend_object_handlers.c:1.69 --- ZendEngine2/zend_object_handlers.c:1.68 Thu Sep 4 12:00:01 2003 +++ ZendEngine2/zend_object_handlers.c Thu Sep 18 07:38:33 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_object_handlers.c,v 1.68 2003/09/04 16:00:01 helly Exp $ */ +/* $Id: zend_object_handlers.c,v 1.69 2003/09/18 11:38:33 helly Exp $ */ #include "zend.h" #include "zend_globals.h" @@ -860,6 +860,28 @@ return SUCCESS; } +int zend_std_cast_object(zval *readobj, zval *writeobj, int type, int should_free TSRMLS_DC) +{ + zval fname, *retval; + + switch (type) { + case IS_STRING: + ZVAL_STRING(&fname, "__tostring", 0); + if (call_user_function_ex(NULL, &readobj, &fname, &retval, 0, NULL, 0, NULL TSRMLS_CC) == SUCCESS) { + if (Z_TYPE_P(retval) != IS_STRING) { + zend_error(E_ERROR, "Method %s::__toString() must return a string value", Z_OBJCE_P(readobj)->name); + } + ZVAL_STRING(writeobj, Z_STRVAL_P(retval), 1); + zval_ptr_dtor(&retval); + return SUCCESS; + } + break; + default: + break; + } + return FAILURE; +} + zend_object_handlers std_object_handlers = { zend_objects_store_add_ref, /* add_ref */ zend_objects_store_del_ref, /* del_ref */ @@ -884,7 +906,7 @@ zend_std_object_get_class, /* get_class_entry */ zend_std_object_get_class_name, /* get_class_name */ zend_std_compare_objects, /* compare_objects */ - NULL, /* cast_object */ + zend_std_cast_object, /* cast_object */ }; /* Index: ZendEngine2/zend_object_handlers.h diff -u ZendEngine2/zend_object_handlers.h:1.23 ZendEngine2/zend_object_handlers.h:1.24 --- ZendEngine2/zend_object_handlers.h:1.23 Sat Aug 16 16:12:01 2003 +++ ZendEngine2/zend_object_handlers.h Thu Sep 18 07:38:33 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_object_handlers.h,v 1.23 2003/08/16 20:12:01 helly Exp $ */ +/* $Id: zend_object_handlers.h,v 1.24 2003/09/18 11:38:33 helly Exp $ */ #ifndef ZEND_OBJECT_HANDLERS_H #define ZEND_OBJECT_HANDLERS_H @@ -82,7 +82,7 @@ typedef zend_class_entry *(*zend_object_get_class_entry_t)(zval *object TSRMLS_DC); typedef int (*zend_object_get_class_name_t)(zval *object, char **class_name, zend_uint *class_name_len, int parent TSRMLS_DC); typedef int (*zend_object_compare_t)(zval *object1, zval *object2 TSRMLS_DC); -typedef void (*zend_object_cast_t)(zval *readobj, zval *writeobj, int type, int should_free TSRMLS_DC); +typedef int (*zend_object_cast_t)(zval *readobj, zval *writeobj, int type, int should_free TSRMLS_DC); typedef struct _zend_object_handlers { Index: ZendEngine2/zend_operators.c diff -u ZendEngine2/zend_operators.c:1.162 ZendEngine2/zend_operators.c:1.163 --- ZendEngine2/zend_operators.c:1.162 Mon Sep 15 04:13:34 2003 +++ ZendEngine2/zend_operators.c Thu Sep 18 07:38:33 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_operators.c,v 1.162 2003/09/15 08:13:34 zeev Exp $ */ +/* $Id: zend_operators.c,v 1.163 2003/09/18 11:38:33 helly Exp $ */ #include <ctype.h> @@ -488,14 +488,20 @@ break; case IS_OBJECT: if (op->value.obj.handlers->cast_object) { + zval tmp; TSRMLS_FETCH(); - op->value.obj.handlers->cast_object(op, op, IS_STRING, 1 TSRMLS_CC); + if (op->value.obj.handlers->cast_object(op, &tmp, IS_STRING, 1 TSRMLS_CC) == SUCCESS) { + zval_dtor(op); + *op = tmp; + break; + } + zend_error(E_NOTICE, "Object of class %s could not be converted to string", Z_OBJCE_P(op)->name); } else { - zval_dtor(op); - op->value.str.val = estrndup_rel("Object", sizeof("Object")-1); - op->value.str.len = sizeof("Object")-1; - zend_error(E_NOTICE, "Object to string conversion"); + zend_error(E_NOTICE, "Object of class %s to string conversion", Z_OBJCE_P(op)->name); } + zval_dtor(op); + op->value.str.val = estrndup_rel("Object", sizeof("Object")-1); + op->value.str.len = sizeof("Object")-1; break; default: zval_dtor(op); Index: php-src/tests/classes/tostring.phpt +++ php-src/tests/classes/tostring.phpt --TEST-- ZE2 __toString() --SKIPIF-- <?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 needed'); ?> --FILE-- <?php class test1 { } class test2 { function __toString() { return "Converted"; } } echo "====test1====\n"; $o = new test1; print_r($o); var_dump((string)$o); var_dump($o); echo "====test2====\n"; $o = new test2; print_r($o); var_dump((string)$o); var_dump($o); echo "====done!====\n"; ?> --EXPECTF-- ====test1==== test1 Object ( ) Notice: Object of class test1 could not be converted to string in %stostring.php on line %d string(6) "Object" object(test1)#%d (0) { } ====test2==== test2 Object ( ) string(9) "Converted" object(test2)#%d (0) { } ====done!====

« previous php.zend-engine.cvs (#1846) next »