]> granicus.if.org Git - php/commitdiff
- Make sure that typehints for classes can only accept "= NULL" as default
authorDerick Rethans <derick@php.net>
Mon, 17 Oct 2005 07:03:33 +0000 (07:03 +0000)
committerDerick Rethans <derick@php.net>
Mon, 17 Oct 2005 07:03:33 +0000 (07:03 +0000)
  value.

Zend/zend_compile.c
tests/lang/type_hints_002.phpt [new file with mode: 0644]
tests/lang/type_hints_003.phpt [new file with mode: 0644]

index cae7f11c1336016ef02fb771049713cd6efd9888..fdda529d5a3c0417161f5478b6b1468fc1e7913c 100644 (file)
@@ -1332,12 +1332,20 @@ void zend_do_receive_arg(zend_uchar op, znode *var, znode *offset, znode *initia
                if (class_type->u.constant.type == IS_STRING || class_type->u.constant.type == IS_UNICODE) {
                        cur_arg_info->class_name = Z_UNIVAL(class_type->u.constant);
                        cur_arg_info->class_name_len = Z_UNILEN(class_type->u.constant);
+
+                       /* FIXME: make this work for unicode=on too */
+                       if (op == ZEND_RECV_INIT) {
+                               if (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
+                                       cur_arg_info->allow_null = 1;
+                               } else {
+                                       zend_error(E_COMPILE_ERROR, "Default value for parameters with a class type hint can only be NULL");
+                               }
+                       }
                } else {
                        cur_arg_info->array_type_hint = 1;
                        cur_arg_info->class_name = NULL;
                        cur_arg_info->class_name_len = 0;
                }
-               cur_arg_info->allow_null = (op == ZEND_RECV_INIT && (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL")))) ? 1 : 0;
        } else {
                cur_arg_info->class_name = NULL;
                cur_arg_info->class_name_len = 0;
diff --git a/tests/lang/type_hints_002.phpt b/tests/lang/type_hints_002.phpt
new file mode 100644 (file)
index 0000000..b21240a
--- /dev/null
@@ -0,0 +1,28 @@
+--TEST--
+ZE2 type hinting
+--SKIPIF--
+<?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 needed'); ?>
+--FILE--
+<?php
+class P { }
+class T {
+       function f(P $p = NULL) {
+               var_dump($p);
+               echo "-\n";
+       }
+}
+
+$o=new T();
+$o->f(new P);
+$o->f();
+$o->f(NULL);
+?>
+--EXPECT--
+object(P)#2 (0) {
+}
+-
+NULL
+-
+NULL
+-
+
diff --git a/tests/lang/type_hints_003.phpt b/tests/lang/type_hints_003.phpt
new file mode 100644 (file)
index 0000000..9f1d2ba
--- /dev/null
@@ -0,0 +1,14 @@
+--TEST--
+ZE2 type hinting
+--SKIPIF--
+<?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 needed'); ?>
+--FILE--
+<?php
+class T {
+       function f(P $p = 42) {
+       }
+}
+?>
+--EXPECTF--
+
+Fatal error: Default value for parameters with a class type hint can only be NULL in %stest.php on line 3