Skip to content

Protected properties are not scoped according to their prototype #19044

Open
@bwoebi

Description

@bwoebi

Description

The following code (https://3v4l.org/7Xf05#v8.4.10):

<?php

abstract class P {
    abstract protected $foo { get; }
}

class C1 extends P {
    protected $foo = 1;
}

class C2 extends P {
    protected $foo = 2;
    
    static function foo($c) { return $c->foo; }
}

var_dump(C2::foo(new C2));
var_dump(C2::foo(new C1));

Resulted in this output:

int(2)

Fatal error: Uncaught Error: Cannot access protected property C1::$foo in /in/7Xf05:14
Stack trace:
#0 /in/7Xf05(18): C2::foo(Object(C1))
#1 {main}
  thrown in /in/7Xf05 on line 14

But I expected this output instead:

int(2)
int(1)

Doing the same with methods works flawlessly (https://3v4l.org/iQo81#v8.4.10):

<?php

abstract class P {
    abstract protected function foo();
}

class C1 extends P {
    protected function foo() { return 1; }
}

class C2 extends P {
    protected function foo() { return 2; }
    
    static function doFoo($c) { return $c->foo(); }
}

var_dump(C2::doFoo(new C2));
var_dump(C2::doFoo(new C1));

results in the expected

int(2)
int(1)

PHP Version

PHP 8.4.10

Operating System

No response

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