php / php/doc-en

inheritance model not entirely clear in docs

Open
#4,553 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the inheritance section of the PHP manual page linked in the issue and review the proposed Bar/Foo example and its output. Update the explanation so the behavior of private methods and the example are clear, and ensure the documented result matches the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.