Skip to content

new MyClass()->method() without parentheses #13029

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
Closed
64 changes: 64 additions & 0 deletions Zend/tests/new_without_parentheses/anonymous_class_access.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
Immediate access on new anonymous class
--FILE--
<?php

echo new class {
const C = 'constant' . PHP_EOL;
}::C;

echo new class {
const C = 'constant' . PHP_EOL;
}::{'C'};

echo new class {
public $property = 'property' . PHP_EOL;
}->property;

echo new class {
public static $property = 'static property' . PHP_EOL;
}::$property;

new class {
public function method() { echo 'method' . PHP_EOL; }
}->method();

new class {
public static function method() { echo 'static method' . PHP_EOL; }
}::method();

new class {
public function __invoke() { echo '__invoke' . PHP_EOL; }
}();

new class () implements ArrayAccess {
public function offsetExists(mixed $offset): bool { return true; }

public function offsetGet(mixed $offset): mixed { echo 'offsetGet' . PHP_EOL; return null; }

public function offsetSet(mixed $offset, mixed $value): void {}

public function offsetUnset(mixed $offset): void {}
}['key'];

isset(new class () implements ArrayAccess {
public function offsetExists(mixed $offset): bool { echo 'offsetExists' . PHP_EOL; return true; }

public function offsetGet(mixed $offset): mixed { return null; }

public function offsetSet(mixed $offset, mixed $value): void {}

public function offsetUnset(mixed $offset): void {}
}['key']);

?>
--EXPECT--
constant
constant
property
static property
method
static method
__invoke
offsetGet
offsetExists
10 changes: 10 additions & 0 deletions Zend/tests/new_without_parentheses/assign_to_new.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Cannot assign to new expression
--FILE--
<?php

new ArrayObject() = 1;

?>
--EXPECTF--
Parse error: syntax error, unexpected token "=" in %s on line %d
26 changes: 26 additions & 0 deletions Zend/tests/new_without_parentheses/garbage_collection.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Object instantiated without parentheses is collected
--FILE--
<?php

class A
{
public function test(): void
{
echo 'called' . PHP_EOL;
}

public function __destruct()
{
echo 'collected' . PHP_EOL;
}
}

new A()->test();
echo 'code after' . PHP_EOL;

?>
--EXPECT--
called
collected
code after
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
A function and a class with the same name work as expected
--FILE--
<?php

function Something(): string
{
return 'Another';
}

class Something {}

class Another {}

echo Something() . PHP_EOL;
var_dump(new Something());
var_dump(new (Something()));

?>
--EXPECT--
Another
object(Something)#1 (0) {
}
object(Another)#1 (0) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
--TEST--
Immediate access on new object with constructor arguments' parentheses
--FILE--
<?php

class A implements ArrayAccess
{
const C = 'constant' . PHP_EOL;
public $property = 'property' . PHP_EOL;
public static $staticProperty = 'static property' . PHP_EOL;

public function __invoke(): void
{
echo '__invoke' . PHP_EOL;
}

public function method(): void
{
echo 'method' . PHP_EOL;
}

public static function staticMethod(): void
{
echo 'static method' . PHP_EOL;
}

public function offsetExists(mixed $offset): bool
{
echo 'offsetExists' . PHP_EOL;

return true;
}

public function offsetGet(mixed $offset): mixed
{
echo 'offsetGet' . PHP_EOL;

return null;
}

public function offsetSet(mixed $offset, mixed $value): void {}

public function offsetUnset(mixed $offset): void {}
}

$class = A::class;

echo new A()::C;
echo new A()::{'C'};
echo new $class()::C;
echo new $class()::{'C'};
echo new (trim(' A '))()::C;
echo new (trim(' A '))()::{'C'};

echo new A()->property;
echo new $class()->property;
echo new (trim(' A '))()->property;

echo new A()::$staticProperty;
echo new $class()::$staticProperty;
echo new (trim(' A '))()::$staticProperty;

new A()();
new $class()();
new (trim(' A '))()();

new A()->method();
new $class()->method();
new (trim(' A '))()->method();

new A()::staticMethod();
new $class()::staticMethod();
new (trim(' A '))()::staticMethod();

new A()['key'];
new $class()['key'];
new (trim(' A '))()['key'];

isset(new A()['key']);
isset(new $class()['key']);
isset(new (trim(' A '))()['key']);

?>
--EXPECT--
constant
constant
constant
constant
constant
constant
property
property
property
static property
static property
static property
__invoke
__invoke
__invoke
method
method
method
static method
static method
static method
offsetGet
offsetGet
offsetGet
offsetExists
offsetExists
offsetExists
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Immediate array access on new object without constructor parentheses
--FILE--
<?php

class A extends ArrayObject
{
}

echo new A['test'];

?>
--EXPECTF--
Parse error: syntax error, unexpected token "[", expecting "," or ";" in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Immediate constant access on new object without constructor parentheses
--FILE--
<?php

class A
{
const C = 'constant';
}

echo new A::C;

?>
--EXPECTF--
Parse error: syntax error, unexpected identifier "C", expecting variable or "$" in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Immediate method call on new object without constructor parentheses
--FILE--
<?php

class A
{
public function test(): void
{
echo 'called';
}
}

new A->test();

?>
--EXPECTF--
Parse error: syntax error, unexpected token "->" in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Immediate property access on new object without constructor parentheses
--FILE--
<?php

class A
{
public $prop = 'property';
}

echo new A->prop;

?>
--EXPECTF--
Parse error: syntax error, unexpected token "->", expecting "," or ";" in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Immediate static method call on new object without constructor parentheses
--FILE--
<?php

class A
{
public static function test(): void
{
echo 'called';
}
}

new A::test();

?>
--EXPECTF--
Parse error: syntax error, unexpected identifier "test", expecting variable or "$" in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Immediate static property access on new object without constructor parentheses
--FILE--
<?php

class B
{
}

class A
{
public static $prop = B::class;
}

var_dump(new A::$prop);

?>
--EXPECT--
object(B)#1 (0) {
}
10 changes: 10 additions & 0 deletions Zend/tests/new_without_parentheses/unset_new.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Cannot unset new expression
--FILE--
<?php

unset(new ArrayObject());

?>
--EXPECTF--
Parse error: syntax error, unexpected token ")", expecting "->" or "?->" or "{" or "[" in %s on line %d
Loading
Loading