Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1074203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:05:27+00:00 2026-05-16T21:05:27+00:00

Consider the following piece of code: class foo { private function m() { echo

  • 0

Consider the following piece of code:

class foo {
    private function m() {
        echo 'foo->m() ';
    }
    public function call() {
        $this->m();
    }
}

class bar extends foo {
    private function m() {
        echo 'bar->m() ';
    }
    public function callbar() {
        $this->m();
    }
}

$bar = new bar;

$bar->call();
$bar->callbar();

Now, changing the visibility of the m() method, I get:
(+ for public, - for private)

Visibility              bar->call()    bar->callbar() 
======================================================
-foo->m(), -bar->m()    foo->m()       bar->m()
-foo->m(), +bar->m()    foo->m()       bar->m()
+foo->m(), -bar->m()    ERROR          ERROR
+foo->m(), +bar->m()    bar->m()       bar->m()

(protected seems to behave like public).

I was expecting everything to behave like it does when both are declared public. But although foo->call() and bar->callbar() are essentially the same thing, they yield different results depending on the visibility of m() in foo and bar. Why does this happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:05:28+00:00Added an answer on May 16, 2026 at 9:05 pm

    Inheriting/overriding private methods

    In PHP, methods (including private ones) in the subclasses are either:

    • Copied; the scope of the original function is maintained.
    • Replaced (“overridden”, if you want).

    You can see this with this code:

    <?php
    class A {
        //calling B::h, because static:: resolves to B::
        function callH() { static::h(); }
        private function h() { echo "in A::h"; }
    }
    class B extends A {
        //not necessary; just to make explicit what's happening
        function callH() { parent::callH(); }
    }
    $b = new B;
    $b->callH();
    

    Now if you override the private method, its new scope will not be A, it will be B, and the call will fail because A::callH() runs in scope A:

    <?php
    class A {
        //calling B::h, because static:: resolves to B::
        function callH() { static::h(); }
        private function h() { echo "in A::h"; }
    }
    class B extends A {
        private function h() { echo "in B::h"; }
    }
    $b = new B;
    $b->callH(); //fatal error; call to private method B::h() from context 'A'
    

    Calling methods

    Here the rules are as follows:

    • Look in the method table of the actual class of the object (in your case, bar).
      • If this yields a private method:
        • If the scope where the method was defined is the same as the scope of the calling function and is the same as the class of the object, use it.
        • Otherwise, look in the parent classes for a private method with the same scope as the one of the calling function and with the same name.
        • If no method is found that satisfies one of the above requirements, fail.
      • If this yields a public/protected method:
        • If the scope of the method is marked as having changed, we may have overridden a private method with a public/protected method. So in that case, and if, additionally, there’s a method with the same name that is private as is defined for the scope of the calling function, use that instead.
        • Otherwise, use the found method.

    Conclusion

    1. (Both private) For bar->call(), the scope of call is foo. Calling $this->m() elicits a lookup in the method table of bar for m, yielding a private bar::m(). However, the scope of bar::m() is different from the calling scope, which foo. The method foo:m() is found when traversing up the hierarchy and is used instead.
    2. (Private in foo, public in bar) The scope of call is still foo. The lookup yields a public bar::m(). However, its scope is marked as having changed, so a lookup is made in the function table of the calling scope foo for method m(). This yields a private method foo:m() with the same scope as the calling scope, so it’s used instead.
    3. Nothing to see here, error because visibility was lowered.
    4. (Both public) The scope of call is still foo. The lookup yields a public bar::m(). Its scope isn’t marked as having changed (they’re both public), so bar::m() is used.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following piece of code - class MyThread extends Thread { private int
Consider the following piece of code #include<iostream> #include<string> class A { private: char name[10];
Consider the following piece of code : <select> <option value=0>foo</option> <option value=1 selected=selected>bar</option> </select>
Consider the following piece of code: final Foo foo = context.mock(Foo.class); context.checking(new Expectations() {{
Consider the following piece of code: class B { private: // some data members
Consider the following piece of code: public class Article : AbstractEntity<Article> { // ...
Consider the following piece of C++0x code: a_signal.connect([](int i) { if(boost::any_cast<std::string>(_buffer[i]) == foo) {
Consider the following piece of code: function processParagraph(paragraph) { if (paragraph.charAt(0) === '%') {
Consider following piece of code void foo( bool forwad ) { vector<MyObject>::iterator it, end_it;
Consider the following piece of code: case class User(id: Int, name: String) object User{

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.