Skip to content

inheritance model not entirely clear in docs #4553

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

Open
visualex opened this issue Mar 21, 2025 · 0 comments
Open

inheritance model not entirely clear in docs #4553

visualex opened this issue Mar 21, 2025 · 0 comments

Comments

@visualex
Copy link

From manual page: https://php.net/language.oop5.inheritance

regarding the section:

Private methods of a parent class are not accessible to a child class. As a result, child classes may reimplement a private method themselves without regard for normal inheritance rules.

I would like to improve the documentation by adding the following example:

class Bar {
    private function one() {
        echo "Bar::one()\n";
    }
    public function run() {
        echo "Bar::run()\n";
        $this->one();
    }
}
class Foo extends Bar {
    private function one() {
        echo "Foo::one()\n";
    }
}
(new Foo())->run();

the output is

Bar::run()
Bar::one()

Which is not really what I would expect to happen since I'm calling Foo, which inherits ::run() and I expected run() to happen within Foo and not Bar. So I presume this is a feature, but how do I extend Bar without modifying it (let's say I cannot) and override ::one() ?

The solution I can see so far is that both need to be public.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant