[php-src] Issue #14009: Trait method has limited visibility to parent class
| From: | mvorisek | Date: | Fri, 19 Apr 2024 19:13:47 +0000 |
| Subject: | [php-src] Issue #14009: Trait method has limited visibility to parent class | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246794@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/14009
Author: mvorisek
### Description
The following code:
```php
<?php
abstract class Expression
{
protected function escapeStringLiteral(string $value): string
{
return 'never called';
}
}
trait ExpressionTrait
{
#[\Override]
protected function escapeStringLiteral(string $value): string
{
return '\'' . str_replace('\'', '\'\'',
$value) . '\'';
}
}
class MyExpression extends Expression
{
use ExpressionTrait;
}
class TestExpression extends Expression {
private Expression $dummyExpression;
public function __construct(Expression $dummyExpression)
{
$this->dummyExpression = $dummyExpression;
}
#[\Override]
protected function escapeStringLiteral(string $value): string
{
return $this->dummyExpression->escapeStringLiteral($value);
}
public function test(): void
{
var_dump($this->escapeStringLiteral('foo \' bar'));
}
};
class Test
{
public function test(): void
{
$exprNoRender = new TestExpression(new MyExpression());
$exprNoRender->test();
}
}
(new Test)->test();
```
online repro: https://3v4l.org/6u6sk
Resulted in this output:
```
Fatal error: Uncaught Error: Call to protected method MyExpression::escapeStringLiteral() from scope
TestExpression in /in/6u6sk:36
Stack trace:
#0 /in/6u6sk(41): TestExpression->escapeStringLiteral('foo ' bar')
#1 /in/6u6sk(51): TestExpression->test()
#2 /in/6u6sk(55): Test->test()
#3 {main}
thrown in /in/6u6sk on line 36
```
But I expected this output instead:
```
string(12) "'foo '' bar'"
```
### PHP Version
any
### Operating System
any