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 8641903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:38:54+00:00 2026-06-12T11:38:54+00:00

I have confused from an example in php manual. It’s about visibility. Here is

  • 0

I have confused from an example in php manual. It’s about visibility. Here is the example.

class Bar {
    public function test() {
        $this->testPrivate();
        $this->testPublic();
    }
    public function testPublic() {
        echo "Bar::testPublic\n";
    }
    private function testPrivate() {
        echo "Bar::testPrivate\n";
    }
}
class Foo extends Bar {
    public function testPublic() {
        echo "Foo::testPublic\n";
    }
    private function testPrivate() {
        echo "Foo::testPrivate\n";
    }
}
$myFoo = new foo();
$myFoo->test();  
?>

http://www.php.net/manual/en/language.oop5.visibility.php

This example outputs

Bar::testPrivate 
Foo::testPublic

Please can you explain how this happen?

why both testPublic() are not called?

I put a var_dump($this) in Bar class construct. It prints object(Foo)[1]. The thing I know is private properties can be called withing same class.

Then how “Bar::testPrivate” is called?

  • 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-06-12T11:38:55+00:00Added an answer on June 12, 2026 at 11:38 am

    Then how “Bar::testPrivate” is called?

    When you call $myFoo->test(), it runs the code in the context of Bar because the Foo class didn’t override it.

    Inside Bar::test(), when $this->testPrivate() gets called, the interpreter will look at Foo first but that method is private (and private methods from descendent classes can’t be called from Bar), so it goes one level up until it can find a suitable method; in this case that would be Bar::testPrivate().

    In contrast, when $this->testPublic() gets called, the interpreter immediately finds a suitable method in Foo and runs it.

    Edit

    why both testPublic() are not called?

    Only one method gets called when you run $this->testPublic(), the furthest one (in terms of distance to the base class).

    If Foo::testPublic() needs to also execute the parent’s implementation, you should write parent::testPublic() inside that method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never code in Python and trying to switch from Javascrpt/SVG. Being confused
I have a rather confusing problem. I have a php file (http://example.com/delete.php) <?php session_start();
I have a rather confusing problem. I have a php file (http://example.com/delete.php) <?php session_start();
I have a two dimensional JSON array returned from a PHP script via a
I have problem with date function in php. If I supply string in format
I got really confused when i tried to transfer variables from php to js.
I have managed to confuse myself whether I should return E_NOTIMPL or E_NOINTERFACE from
I seem to have gotten confused as to what the css clear keyword means.
I am currently working with Python and have been confused over the fact that
In C++, the following lines have me confused: int temp = (int)(0×00); int temp

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.