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

  • Home
  • SEARCH
  • 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 8727671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:28:44+00:00 2026-06-13T08:28:44+00:00

I have difficulties in understanding why we get the output of this code: <?php

  • 0

I have difficulties in understanding why we get the output of this code:

<?php

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(); 
?>

So Foo extends Bar. $myfoo is an obect of the class Foo. Foo doesn’t have a method, called test(), so it extends it from its parent Bar. But why the result of test() is

Bar::testPrivate 
Foo::testPublic

Can you please explain me why the first isn’t Foo::testPrivate, when this parent’s method is overridden in the child?

Thank you very much in advance!

  • 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-13T08:28:45+00:00Added an answer on June 13, 2026 at 8:28 am

    Probably becuase testPrivate is, as the name already suggests, a private method and won’t be inherited / overwritten by class inheritance.

    On the php.net manual page you probably got that code from it explicitely states that We can redeclare the public and protected method, but not private

    So, what exactly happens is the following: The child class will not redeclare the method testPrivate but instead creates it’s own version in the “scope” if the child object only. As test() is defined in the parent class, it will access the parents testPrivate.

    If you would redeclare the test function in the child class, it should access the childs? testPrivate() method.

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

Sidebar

Related Questions

I have difficulties in binding the follow structures to XAML View: public class SampleViewModel
I have some difficulties in understanding a function Win32_AllocatedResource() and its fields antecedent and
I have difficulties understanding a problem I encountered during instantiation of a QT class
i have some understanding difficulties. i have some checkboxes in html like this: <input
I am reading the book: Intel Threading Building Blocks. I often have difficulties understanding
I have some difficulties with understanding BindingSource's behaviour. Let's look at following example: Creating
I have difficulties developing 4x1 widget for Android. This is the appprovider <?xml version=1.0
I know this question had been asked a hundred times, but I have difficulties
I have a lot of difficulties in trying to translate this SQL query to
I am converting my PHP/mySQL code to PDO but I am having some difficulties.

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.