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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:11:26+00:00 2026-05-27T06:11:26+00:00

I have a class that is extended, and its children extended further, an arbitrary

  • 0

I have a class that is extended, and its children extended further, an arbitrary number of times. Each extension provides more functionality than its predecessor. The catch is, not all parameters can be provided at initialization time. It is complex enough to warrant passing more configuration options via methods, and then calling some sort of build() or configure() method to ready itself for operation.

The catch is, each class in the hierarchy needs a chance to configure itself, and it needs to cascade from the parent to all of the children.

I successfully do this below, but this solution requires that each class remember to call it’s parent’s method, otherwise it breaks. I want to remove that responsibility from those who might forget to do such.

How do I modify this code to achieve this?

<?php

class A {
    function configure() {
        print("I am A::configure() and provide basic functionality.<br/>\n");;
    }
}

class B extends A {
    function configure() {
        parent::configure();
        print("I am B::configure() and provide additional functionality.<br/>\n");
    }
}

class C extends B {
    function configure() {
        parent::configure();
        print("I am C::configure() and provide even more functionality.<br/>\n");
    }
}

$c = new C;
$c->configure();

?>

Output:

I am A::configure() and provide basic functionality.
I am B::configure() and provide additional functionality.
I am C::configure() and provide even more functionality.

Thanks for your help! 🙂

  • 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-27T06:11:27+00:00Added an answer on May 27, 2026 at 6:11 am

    Without claiming it’s pretty, I’d suggest the following. I left out your configure() functions for brevity.

    <?php
    
    class A {
      function configure_master() {
        $init_class = get_class($this);
        $ancestry = array();
        while (! empty($init_class)) {
          $ancestry[] = $init_class;
          $init_class = get_parent_class($init_class);
        }
        for ($i = count($ancestry) - 1; $i >= 0; $i--) {
          call_user_func(array($this, $ancestry[$i] . '::configure'));
        }
      }
    }
    
    $c = new C;
    $c->configure_master();
    

    I tested this, and it works. call_user_func($ancestry[$i] . '::configure') also works (at least in php 5.3, which is where I tested it), but it relies on the odd (to me) scoping of $this. I’m uncertain whether this is reliable or will carry forward into future versions. The version above “should be” more robust, given my limited knowledge.

    If keeping the outside call as configure() is a requirement, I’d suggest renaming all your “configure()” methods to “configure_local()” or some such, and rename “configure_master()” to “configure()”. In fact, that’s what I’d do to start with, but that’s a matter of personal taste.

    If you need both the internal method and the external API to remain the same… Then you’re stuck, since the base class method is overridden, but I’m sure you knew that.

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

Sidebar

Related Questions

I have to open a page from class extended from javax.swing.AbstractAction class... Is that
I have an extended dialog class that I want to show for 3 seconds
I have an extended BaseAdapter that has LinearLayout children (an ImageView and a TextView
I have a activity that extends listactivity, extended in this class i have a
In my program I have a class GraphDisplay extended from JPanel that is used
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that looks like this: class A { public: class variables_map vm
I have class that represents users. Users are divided into two groups with different
I have class grades that I need to check if a specific grade is
I have class A such that: class A { static int i; A(); f1();

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.