Skip to content

Trait method has limited visibility to parent class method #14009

Closed
@mvorisek

Description

@mvorisek

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions