From d6272d06059d29d14448ca82195c4c6f8cc34d66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fre=CC=81de=CC=81ric=20Hardy?= Date: Thu, 19 Apr 2012 11:11:34 +0200 Subject: [PATCH 1/2] Add expression support with "new" keyword. It's already possible to create class instance with : 1) a string: 2) a variable: 3) a variable variable: This commit add the possibility to use an expression : --- Zend/tests/instanceof_003.phpt | 22 ++++++++++++++++++++++ Zend/tests/new_001.phpt | 19 +++++++++++++++++++ Zend/tests/new_002.phpt | 21 +++++++++++++++++++++ Zend/tests/new_003.phpt | 21 +++++++++++++++++++++ Zend/zend_language_parser.y | 1 + 5 files changed, 84 insertions(+) create mode 100644 Zend/tests/instanceof_003.phpt create mode 100644 Zend/tests/new_001.phpt create mode 100644 Zend/tests/new_002.phpt create mode 100644 Zend/tests/new_003.phpt diff --git a/Zend/tests/instanceof_003.phpt b/Zend/tests/instanceof_003.phpt new file mode 100644 index 000000000000..afc9b43c9f7e --- /dev/null +++ b/Zend/tests/instanceof_003.phpt @@ -0,0 +1,22 @@ +--TEST-- +Testing instanceof operator with expression +--FILE-- + +--EXPECTF-- +bool(true) +bool(false) +Done diff --git a/Zend/tests/new_001.phpt b/Zend/tests/new_001.phpt new file mode 100644 index 000000000000..c2df6c4755ac --- /dev/null +++ b/Zend/tests/new_001.phpt @@ -0,0 +1,19 @@ +--TEST-- +Create a class instance with a string +--FILE-- + +--EXPECTF-- +bool(true) +Foo +Done diff --git a/Zend/tests/new_002.phpt b/Zend/tests/new_002.phpt new file mode 100644 index 000000000000..e282c7fcc7a0 --- /dev/null +++ b/Zend/tests/new_002.phpt @@ -0,0 +1,21 @@ +--TEST-- +Create a class instance from a variable +--FILE-- + +--EXPECTF-- +bool(true) +Foo +Done diff --git a/Zend/tests/new_003.phpt b/Zend/tests/new_003.phpt new file mode 100644 index 000000000000..dbc050ebffb9 --- /dev/null +++ b/Zend/tests/new_003.phpt @@ -0,0 +1,21 @@ +--TEST-- +Create a class instance from an expression +--FILE-- + +--EXPECTF-- +bool(true) +Foo +Done diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index 893e0133efe0..fc8f22867797 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -869,6 +869,7 @@ fully_qualified_class_name: class_name_reference: class_name { zend_do_fetch_class(&$$, &$1 TSRMLS_CC); } | dynamic_class_name_reference { zend_do_end_variable_parse(&$1, BP_VAR_R, 0 TSRMLS_CC); zend_do_fetch_class(&$$, &$1 TSRMLS_CC); } + | '{' expr '}' { zend_do_fetch_class(&$$, &$2 TSRMLS_CC); } ; From 395f4fba537de587a4a704349d643ee96f379302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fre=CC=81de=CC=81ric=20Hardy?= Date: Fri, 20 Apr 2012 12:12:25 +0200 Subject: [PATCH 2/2] Add unit test about "new {expr}(arguments)". --- Zend/tests/new_004.phpt | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Zend/tests/new_004.phpt diff --git a/Zend/tests/new_004.phpt b/Zend/tests/new_004.phpt new file mode 100644 index 000000000000..4331898a61cb --- /dev/null +++ b/Zend/tests/new_004.phpt @@ -0,0 +1,36 @@ +--TEST-- +Create a class instance from an expression which is a string +--FILE-- +arg1 = $arg1; + $this->arg2 = $arg2; + } + + public function getArg1() { return $this->arg1; } + public function getArg2() { return $this->arg2; } +} + +$oo = 'oo'; + +$foo = new {'f' . $oo}('this is arg1', 'this is arg2'); + +var_dump(is_object($foo)); +echo get_class($foo) . PHP_EOL; +var_dump($foo->getArg1()); +var_dump($foo->getArg2()); + +echo 'Done' . PHP_EOL; +?> +--EXPECTF-- +bool(true) +Foo +string(12) "this is arg1" +string(12) "this is arg2" +Done