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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:28:11+00:00 2026-06-10T00:28:11+00:00

I have two abstract classes in an inheritance chain, within what will eventually be

  • 0

I have two abstract classes in an inheritance chain, within what will eventually be a generic library:

abstract class Foo {
    public function baz() {
        echo 'Foo::baz()';
    }

    // other methods here
}

abstract class Bar extends Foo {
    public function baz() {
        echo 'Bar::baz()';
    }
}

These two classes are meant to be extended by developers, and my problem is that I’d like to make it so that neither implementation of the baz() method can be overridden (as they contain strict RFC-compliant code). Making Bar::baz() final is no problem; however, if I make Foo::baz() final, then Bar itself obviously can’t override it either.

PHP 5.4’s traits would likely offer a practical solution, but I can’t drop support for PHP < 5.4 over this. My last resort is to just leave it as-is and use documentation to warn developers not to override this method, but I’d like to find something more concrete, if possible.

Is there any other design I can use to enforce that both methods shouldn’t be overridden, while simultaneously keeping the code DRY (e.g. not removing the inheritance and duplicating all the code)?

  • 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-10T00:28:12+00:00Added an answer on June 10, 2026 at 12:28 am

    This seems like a situation where the idea “Favor composition over inheritance” applies. It’s a little bit repetitive, but it doesn’t involve repetition of implementations and gives you the functionality you want.

    interface Bazr {
        public function baz();
    
        public function myOtherMethod1();
        public function myOtherMethod2();
    }
    
    public class Foo implements Bazr {
      public final function baz() {} 
      public function myOtherMethod1() {/* default implementation of some method */}
      public function myOtherMethod2() {/* yeah */}
    }
    
    public class Bar implements Bazr {
      private parentBazr = null;
      public function __construct() {
        $this->parentBazr = new Foo();
      }
    
      public final function baz() {}
      public function myOtherMethod1() {
        $this->parentBazr->myOtherMethod1();
      }
      public function myOtherMethod2() {
        $this->parentBazr->myOtherMethod1();
      }
    }
    

    Both Foo and Bar can be extended, Bar “composes” Foo’s functionality, and baz is final in both Foo and Bar. I’m not sure if you prefer to include or omit the interface… PHP doesn’t have typed variables so it’s probably not necessary besides for enforcing the contract with the two implementing classes. The disadvantage of defining the interface is that others can then implement the interface (though that would also be the case with a trait).

    Sorry if I mixed java/php syntax, I think I left out some “$”‘s.

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

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend
Say we have a class inheriting from two base classes (multiple inheritance). Base class
I have the following abstract classes: public abstract class AbSuperClass1<K,S> { //class definition }
I have two implemented classes: class DCCmd : public DCMessage class DCReply : public
I have two abstract classes which will hold references to each other.How / can
In my c# project, I have two abstract generic parent classes which then have
I have a singleton class that implements two other abstract classes. My monkey::getMonkey fails
I have 3 classes that build a chain of inheritance. Two of the classes
I have two abstract classes class abstract A { //some methods . } class

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.