]> granicus.if.org Git - php/commitdiff
- Fixed Bug #37811 define not using toString on objects
authorMarcus Boerger <helly@php.net>
Sun, 9 Jul 2006 22:40:10 +0000 (22:40 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 9 Jul 2006 22:40:10 +0000 (22:40 +0000)
Zend/tests/bug37811.phpt [new file with mode: 0755]
Zend/zend_builtin_functions.c

diff --git a/Zend/tests/bug37811.phpt b/Zend/tests/bug37811.phpt
new file mode 100755 (executable)
index 0000000..fa65738
--- /dev/null
@@ -0,0 +1,36 @@
+--TEST--
+Bug #37811 define not using toString on objects
+--FILE--
+<?php
+
+class TestClass
+{
+       function __toString()
+       {
+               return "Foo";
+       }
+}
+
+define("Bar",new TestClass);
+var_dump(Bar);
+define("Baz",new stdClass);
+var_dump(Baz);
+
+?>
+===DONE===
+--EXPECTF--
+string(3) "Foo"
+
+Warning: Constants may only evaluate to scalar values in %sbug37811.php on line %d
+
+Notice: Use of undefined constant Baz - assumed 'Baz' in %sbug37811.php on line %d
+string(3) "Baz"
+===DONE===
+--UEXPECTF--
+unicode(3) "Foo"
+
+Warning: Constants may only evaluate to scalar values in %sbug37811.php on line %d
+
+Notice: Use of undefined constant Baz - assumed 'Baz' in %sbug37811.php on line %d
+unicode(3) "Baz"
+===DONE===
index 3d79c97f6ddc93758a65972d917f391eaed2a2c2..029e3285968547cc990bc8b177258c970814d6d5 100644 (file)
@@ -488,7 +488,7 @@ ZEND_FUNCTION(error_reporting)
    Define a new constant */
 ZEND_FUNCTION(define)
 {
-       zval **var, **val, **non_cs;
+       zval **var, **val, **non_cs, *val_free = NULL;
        int case_sensitive;
        zend_constant c;
 
@@ -515,6 +515,7 @@ ZEND_FUNCTION(define)
                        break;
        }
 
+repeat:
        switch (Z_TYPE_PP(val)) {
                case IS_LONG:
                case IS_DOUBLE:
@@ -524,10 +525,26 @@ ZEND_FUNCTION(define)
                case IS_RESOURCE:
                case IS_NULL:
                        break;
+               case IS_OBJECT:
+                       if (!val_free) {
+                               if (Z_OBJ_HT_PP(val)->get) {
+                                       val_free = *val = Z_OBJ_HT_PP(val)->get(*val TSRMLS_CC);
+                                       goto repeat;
+                               } else if (Z_OBJ_HT_PP(val)->cast_object) {
+                                       ALLOC_INIT_ZVAL(val_free);
+                                       if (Z_OBJ_HT_PP(val)->cast_object(*val, val_free, UG(unicode)?IS_UNICODE:IS_STRING TSRMLS_CC) == SUCCESS) {
+                                               val = &val_free;
+                                               break;
+                                       }
+                               }
+                       }
+                       /* no break */
                default:
                        zend_error(E_WARNING,"Constants may only evaluate to scalar values");
+                       if (val_free) {
+                               zval_ptr_dtor(&val_free);
+                       }
                        RETURN_FALSE;
-                       break;
        }
 
        if (Z_TYPE_PP(var) != (UG(unicode)?IS_UNICODE:IS_STRING)) {
@@ -536,6 +553,9 @@ ZEND_FUNCTION(define)
 
        c.value = **val;
        zval_copy_ctor(&c.value);
+       if (val_free) {
+               zval_ptr_dtor(&val_free);
+       }
        c.flags = case_sensitive; /* non persistent */
        if (Z_TYPE_PP(var) == IS_UNICODE) {
                c.name.u = zend_ustrndup(Z_USTRVAL_PP(var), Z_USTRLEN_PP(var));