Class Invasion Both Ways

<?php

class x {
    private function foo() { echo __METHOD__; }

    function YinX() {
        $y = new y();
        $y->foo();
    }
}

class y extends x {
    function YinY() {
        $this->foo();
    }
}

// here, Y can call private foo();
(new x)->YinX();

// here, Y cannot call foo()
(new y)->YinY();

?>

Class invasion, it when an object access anything it wants in another object of the same class. Thanks to the way PHP checks each object’s perimeter.

The same checks allow the other object to access the current one’s, as seen in that example.

See Also

PHP Features

Last updated: 14 July 2026