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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:09:02+00:00 2026-05-27T01:09:02+00:00

In PHP, I’m trying to reference a method defined in an object’s parent class,

  • 0

In PHP, I’m trying to reference a method defined in an object’s parent class, from a method inherited from the object’s parent class. Here’s the code:

class base_class {
  function do_something() {
    print "base_class::do_something()\n";
  }
  function inherit_this() {
    parent::do_something();
  }
}
class middle_class extends base_class {
  function do_something() {
    print "middle_class::do_something()\n";
  }
}
class top_class extends middle_class {
  function do_something() {
    print "top_class::do_something()\n";
    $this->inherit_this();
  }
}

$obj = new top_class;
$obj->do_something();

The problem is that parent::do_something() in inherit_this() tries to find the parent class of base_class, not the parent of the object’s actual class, and the example above throws an error. Is there something I can write instead of parent::do_something() that would call middle_class::do_something(), and that would still work even in classes that extend (say) top_class?

  • 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-27T01:09:02+00:00Added an answer on May 27, 2026 at 1:09 am

    I’ll start by saying “Thank you” to grrbrr404. He gave me some ideas and got me started in the right direction.

    The solution I finally settled on was the following:

    function inherit_this() {
      $bt = debug_backtrace();
      call_user_func(array($this, get_parent_class($bt[1]['class']) . '::do_something'));
    }
    

    It’s not pretty (I particularly hate calling debug_backtrace() for this), but it keeps the object context set to $this, and handles the case where the method is called from a method somewhere in the middle of the object hierarchy.

    For those who found my example confusing and/or wanted to know “Why would you want to do this?” I apologize, and provide the following additional example, which is hopefully more illustrative and closer to the original problem. It is considerably longer, but hopefully shows why I care about keeping $this set properly, and also shows why I can’t hard-code any particular class name or use $this->method(). Avoiding infinite loops is always a priority with me. 🙂

    class class1_required_type { }
    class class2_required_type { }
    class class3_required_type { }
    class class4_required_type { }
    
    class class1 {
      protected $data = array();
      protected function checkType($name, $value, $requiredType) {
        print "In class1::checkType()\n";
        if (get_class($value) === $requiredType) {
          $backtrace = debug_backtrace();
          call_user_func(array($this, get_parent_class($backtrace[1]['class']) . "::mySet"), $name, $value);
        } else {
          throw new Exception(get_class($this) . "::mySet('" . $name . "') requires an object of type '" . $requiredType . "', but got '" . get_class($value) . "'");
        }
      }
      function mySet($name, $value) {
        print "In class1::mySet()\n";
        if ($name === 'class1_field') {
          $this->checkType($name, $value, 'class1_required_type');
        } else {
          $this->data[$name] = $value;
        }
      }
      function dump() {
        foreach ($this->data as $key => $value) {
          print "$key: " . get_class($value) . "\n";
        }
      }
    }
    
    class class2 extends class1 {
      function mySet($name, $value) {
        print "In class2::mySet()\n";
        if ($name === 'class2_field') {
          $this->checkType($name, $value, 'class2_required_type');
        } else {
          parent::mySet($name, $value);
        }
      }
    }
    
    class class3 extends class2 {
      function mySet($name, $value) {
        print "In class3::mySet()\n";
        if ($name === 'class3_field') {
          $this->checkType($name, $value, 'class3_required_type');
        } else {
          parent::mySet($name, $value);
        }
      }
    }
    
    class class4 extends class3 {
      function mySet($name, $value) {
        print "In class4::mySet()\n";
        if ($name === 'class4_field') {
          $this->checkType($name, $value, 'class4_required_type');
        } else {
          parent::mySet($name, $value);
        }
      }
    }
    
    $obj = new class4;
    $obj->mySet('class3_field', new class3_required_type);
    $obj->dump();
    

    I’m trying to avoid duplication of the “checkType()” function, yet still provide correct behavior even when the hierarchy gets arbitrarily large.

    More elegant solutions are, of course, most welcome.

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

Sidebar

Related Questions

Php code: class build { function panel() { $dummy=Lorem ipsum dolor sit amet, consectetur
PHP/MySQLisolating database access in class - how to handle multiple row Selects Here’s a
PHP code: <a id=a$id onclick=check($id,1) href=javascript:void(0) class=black>Qualify</a> I want to remove the onclick=check($id,1) so
PHP 5.2.15 I am trying to replace {date[F]} with the date function. I have
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
PHP has a very nice function, isset($variableName). It checks if $variableName is already defined
PHP has a var_dump() function which outputs the internal contents of an object, showing
php newbie here..I need some PHP help ideas/examples on how to import data from
PHP functions such as 'array_map' take a callback, which can be a simple function
PHP version 5.3 has been released, and although it looks great, all my code

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.