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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:52:39+00:00 2026-05-31T08:52:39+00:00

Upon building an MVC framework in PHP I ran into a problem which could

  • 0

Upon building an MVC framework in PHP I ran into a problem which could be solved easily using Java style generics. An abstract Controller class might look something like this:

abstract class Controller {

abstract public function addModel(Model $model);

There may be a case where a subclass of class Controller should only accept a subclass of Model. For example ExtendedController should only accept ReOrderableModel into the addModel method because it provides a reOrder() method that ExtendedController needs to have access to:

class ExtendedController extends Controller {

public function addModel(ReOrderableModel $model) {

In PHP the inherited method signature has to be exactly the same so the type hint cannot be changed to a different class, even if the class inherits the class type hinted in the superclass. In java I would simply do this:

abstract class Controller<T> {

abstract public addModel(T model);


class ExtendedController extends Controller<ReOrderableModel> {

public addModel(ReOrderableModel model) {

But there is no generics support in PHP. Is there any solution which would still adhere to OOP principles?

Edit
I am aware that PHP does not require type hinting at all but it is perhaps bad OOP. Firstly it is not obvious from the interface (the method signature) what kind of objects should be accepted. So if another developer wanted to use the method it should be obvious that objects of type X are required without them having to look through the implementation (method body) which is bad encapsulation and breaks the information hiding principle. Secondly because there’s no type safety the method can accept any invalid variable which means manual type checking and exception throwing is needed all over the place!

  • 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-31T08:52:40+00:00Added an answer on May 31, 2026 at 8:52 am

    It appears to work for me (though it does throw a Strict warning) with the following test case:

    class PassMeIn
    {
    
    }
    
    class PassMeInSubClass extends PassMeIn
    {
    
    }
    
    class ClassProcessor
    {
        public function processClass (PassMeIn $class)
        {
            var_dump (get_class ($class));
        }
    }
    
    class ClassProcessorSubClass extends ClassProcessor 
    {
        public function processClass (PassMeInSubClass $class)
        {
            parent::processClass ($class);
        }
    }
    
    $a  = new PassMeIn;
    $b  = new PassMeInSubClass;
    $c  = new ClassProcessor;
    $d  = new ClassProcessorSubClass;
    
    $c -> processClass ($a);
    $c -> processClass ($b);
    $d -> processClass ($b);
    

    If the strict warning is something you really don’t want, you can work around it like this.

    class ClassProcessor
    {
        public function processClass (PassMeIn $class)
        {
            var_dump (get_class ($class));
        }
    }
    
    class ClassProcessorSubClass extends ClassProcessor 
    {
        public function processClass (PassMeIn $class)
        {
            if ($class instanceof PassMeInSubClass)
            {
                parent::processClass ($class);
            }
            else
            {
                throw new InvalidArgumentException;
            }
        }
    }
    
    $a  = new PassMeIn;
    $b  = new PassMeInSubClass;
    $c  = new ClassProcessor;
    $d  = new ClassProcessorSubClass;
    
    $c -> processClass ($a);
    $c -> processClass ($b);
    $d -> processClass ($b);
    $d -> processClass ($a);
    

    One thing you should bear in mind though, this is strictly not best practice in OOP terms. If a superclass can accept objects of a particular class as a method argument then all its subclasses should also be able of accepting objects of that class as well. Preventing subclasses from processing classes that the superclass can accept means you can’t use the subclass in place of the superclass and be 100% confident that it will work in all cases. The relevant practice is known as the Liskov Substitution Principle and it states that, amongst other things, the type of method arguments can only get weaker in subclasses and the type of return values can only get stronger (input can only get more general, output can only get more specific).

    It’s a very frustrating issue, and I’ve brushed up against it plenty of times myself, so if ignoring it in a particular case is the best thing to do then I’d suggest that you ignore it. But don’t make a habit of it or your code will start to develop all kinds of subtle interdependencies that will be a nightmare to debug (unit testing won’t catch them because the individual units will behave as expected, it’s the interaction between them where the issue lies). If you do ignore it, then comment the code to let others know about it and that it’s a deliberate design choice.

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

Sidebar

Related Questions

Here's a very broad question: I've been building a small MVC framework in PHP,
Upon reviewing a bunch of MVC style web applications, I'm noticing that it's common
Upon fixing my earlier problem where, I uninstalled both, JRE AND Java EE, and
I've recently begun building version 2 of my year-old custom MVC framework. It's worked
Lets say I'm building a base class which will be extended upon by the
I have stumbled upon an interesting problem/bug that I eventually solved, but the solution
I am building a menu in jQuery using .animate() but I've run into a
I'm building a Rails app which creates a bookmarklet file for each user upon
I am building a web application that is dependent upon several third-party libraries. What
Im building a small MVC system and i want my controllers to be Singletons.

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.