Skip to content

Add expression support with "new" keyword. #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Zend/tests/instanceof_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Testing instanceof operator with expression
--FILE--
<?php

class Foo {
}

$foo = new Foo;

$oo = 'oo';
$ar = 'ar';

var_dump($foo instanceof {'f' . $oo});
var_dump($foo instanceof {'b' . $ar});

echo 'Done' . PHP_EOL;
?>
--EXPECTF--
bool(true)
bool(false)
Done
19 changes: 19 additions & 0 deletions Zend/tests/new_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Create a class instance with a string
--FILE--
<?php

class Foo {
}

$foo = new Foo;

var_dump(is_object($foo));
echo get_class($foo) . PHP_EOL;

echo 'Done' . PHP_EOL;
?>
--EXPECTF--
bool(true)
Foo
Done
21 changes: 21 additions & 0 deletions Zend/tests/new_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Create a class instance from a variable
--FILE--
<?php

class Foo {
}

$fooClass = 'Foo';

$foo = new $fooClass;

var_dump(is_object($foo));
echo get_class($foo) . PHP_EOL;

echo 'Done' . PHP_EOL;
?>
--EXPECTF--
bool(true)
Foo
Done
21 changes: 21 additions & 0 deletions Zend/tests/new_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Create a class instance from an expression
--FILE--
<?php

class Foo {
}

$oo = 'oo';

$foo = new {'f' . $oo};

var_dump(is_object($foo));
echo get_class($foo) . PHP_EOL;

echo 'Done' . PHP_EOL;
?>
--EXPECTF--
bool(true)
Foo
Done
36 changes: 36 additions & 0 deletions Zend/tests/new_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Create a class instance from an expression which is a string
--FILE--
<?php

class Foo {
protected $arg1 = null;
protected $arg2 = null;

public function __construct($arg1, $arg2)
{
$this->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
1 change: 1 addition & 0 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
;


Expand Down