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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:03:32+00:00 2026-05-17T21:03:32+00:00

<?php class a { public function foo() { echo __METHOD__ . PHP_EOL; } }

  • 0
<?php
class a {
    public function foo() {
        echo __METHOD__ . PHP_EOL;
    }
}
class b extends a {
    public function foo() {
        $this->foo(); # I want to call a's foo method here.
        echo __METHOD__ . PHP_EOL;
    }
}
class c extends a {
    public function foo() {
        echo __METHOD__ . PHP_EOL;
        $this->foo(); # I want to call a's foo method here.
    }
}
class d extends a {
    # This class does not handle anything super special in our foo step, so I don't over write a's foo();
}
$a = new a; $a->foo();
$b = new b; $b->foo();
$c = new c; $c->foo();
$d = new d; $d->foo();
?>

I’m sure there are a few of you scratching your head trying to figure out why I would ever want to do something like this, so I promise you this will make sense, just let me explain.

I have a function that works just fine by it’s self and I have some other classes that extend upon the base class to add some functions to it, these classes do the same task but in a different context or with additional information so they must do all of the steps of the parent class but they must also do some special sub set’s in some cases.

Is there a way I can still execute a’s foo method and my sub classes foo method?

I know I could fix this with some awful hacks like making the method a file, and then including that file in both the parent function and it’s siblings where needed, or I could make a second method called bar() that would contain the instructions of foo() and have my sub classes overwrite foo() but call bar() when they need the functionality of foo(). I’m just wondering if I can do this without using some pretty interesting hacks, is there something I don’t know about PHP that someone on here can teach me?

  • 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-17T21:03:32+00:00Added an answer on May 17, 2026 at 9:03 pm

    You could try parent::foo();

    in the c class:

    public function bar(){
      return parent::foo();
    }
    

    See Example 3 in the PHP Manual on Scope Resolution

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

Sidebar

Related Questions

<?php class Box { var $contents; function Box($contents) { $this-&gt;contents = $contents; } function
I'm getting a Call to a member function process_data() on a non-object in page.class.php
I currently have my PHP class variables set up like this: class someThing {
I'm looking for a PHP library/function/class which can create Identicon s.
Is there a PHP class/library that would allow me to query an XHTML document
I have a PHP class that creates a PNG image on the fly and
I'd like to be able to write a PHP class that behaves like an
I am using the singleton pattern in all my PHP class files. Would it
I'm searching a small Java based RSS/Feed Generator, something like the FeedCreator.class.php library, any
I am using the PHP 5 Tidy class to format html. Everything is fine

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.