Skip to content
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

Trait method has limited visibility to parent class method #14009

Closed
mvorisek opened this issue Apr 19, 2024 · 1 comment
Closed

Trait method has limited visibility to parent class method #14009

mvorisek opened this issue Apr 19, 2024 · 1 comment

Comments

@mvorisek
Copy link
Contributor

mvorisek commented Apr 19, 2024

Description

The following code:

<?php

abstract class Expression
{
    protected function escapeStringLiteral(string $value): string
    {
        return 'never called';
    }
}

trait ExpressionTrait
{
    // when this method is commented out, the repro works as expected (protected method can be accessed)
    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 TestCl
{
    public function test(): void
    {
        $exprNoRender = new TestExpression(new MyExpression());
        
        $exprNoRender->test();
    }
}

(new TestCl())->test();

online repro: https://3v4l.org/KI9eF

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): TestCl->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

@mvorisek mvorisek changed the title Trait method has limited visibility to parent class Trait method has limited visibility to parent class method Apr 19, 2024
@iluuu1994
Copy link
Member

iluuu1994 commented Apr 20, 2024

Simplified example: https://3v4l.org/1F3NA I find this behavior odd to begin with (since A is not actually directly related to B but can still call its overridden, protected method) but trait methods should behave more or less equivalently to directly declared methods.

iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 20, 2024
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 20, 2024
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 22, 2024
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 22, 2024
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 22, 2024
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 23, 2024
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 23, 2024
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 23, 2024
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 23, 2024
dstogov pushed a commit to dstogov/php-src that referenced this issue May 6, 2024
@dstogov dstogov closed this as completed in c6b75f9 May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants