Skip to content

Redeclaring protected properties on children creates a liskov violation #19406

Description

@jnvsor

Description

The following code:

<?php
class P {
    protected $v;
}

class C extends P {
    public function test(P $i) {
        return $i->v;
    }
}

class C2 extends P {
    protected $v;
}

$c = new C;
$c2 = new C2;
$c->test($c2);

Resulted in this output:

PHP Fatal error:  Uncaught Error: Cannot access protected property C2::$v

But I expected this output instead:


After a discussion on the mailing list everyone seemed to agree it's a bug, and I believe fixing it wouldn't break any pre-existing code. Properties were invariant up to 8.4, and property hook variance is well defined.

PHP Version

PHP 8.4.10 (cli) (built: Jul  3 2025 12:35:27) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.4.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.10, Copyright (c), by Zend Technologies
    with Xdebug v3.4.3, Copyright (c) 2002-2025, by Derick Rethans

Operating System

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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