Skip to content

Test anonymous class in trait must be declared only once #10396

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
43 changes: 43 additions & 0 deletions Zend/tests/anon/017.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Anonymous class in trait must be declared only once
--FILE--
<?php

trait TraitWithAnonymousClass
{
public function foo(): object
{
return new class() {};
}
}

$c = count(get_declared_classes());

if (time() > 0) {
class A { use TraitWithAnonymousClass; }
var_dump(count(get_declared_classes()) - $c);

$o = new A();
var_dump(count(get_declared_classes()) - $c);

$o->foo();
var_dump(count(get_declared_classes()) - $c);
}

eval('class B { use TraitWithAnonymousClass; }');
var_dump(count(get_declared_classes()) - $c);

$o = new B();
var_dump(count(get_declared_classes()) - $c);

$o->foo();
var_dump(count(get_declared_classes()) - $c);

?>
--EXPECT--
int(1)
int(1)
int(1)
int(2)
int(2)
int(2)
45 changes: 45 additions & 0 deletions Zend/tests/static_variables_parent_trait_method.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--TEST--
Behavior of static variables in parent trait method
--FILE--
<?php

trait T {
public function foo()
{
var_dump(__CLASS__);

static $v = null;
if ($v === null) {
$v = random_bytes(64);
}

return $v;
}
}

class A { use T; }
class B extends A {}
class C extends A { use T; }
class D { use T; }

$a = (new A())->foo();
var_dump($a === (new A())->foo());
var_dump($a === (new B())->foo());
$c = new C();
var_dump($a === $c->foo());
var_dump($a === (new \ReflectionMethod(A::class, 'foo'))->invoke($c));
var_dump($a === (new D())->foo());

?>
--EXPECT--
string(1) "A"
string(1) "A"
bool(true)
string(1) "A"
bool(true)
string(1) "C"
bool(false)
string(1) "A"
bool(true)
string(1) "D"
bool(false)