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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:09:37+00:00 2026-06-03T05:09:37+00:00

I would like to have an interface that allows for a generic type public

  • 0

I would like to have an interface that allows for a generic type

public function persist($object);

But my concrete implementations to have a type

public function persist(User $user);

From what I understand of PHP this is not possible. From an object oriented design point of view could someone explain to me why what I am doing is misguided and wrong.

Edit: I should clarify, I’m aware of type hinting and how it works my question is really trying to understand from an OO perspective where I am going wrong when I want my concrete implementation to take a different type to the interface.

  • 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-03T05:09:38+00:00Added an answer on June 3, 2026 at 5:09 am

    The interface’s purpose is to be a contract between classes. An interface would be useless if multiple concrete classes implemented it, but all expected different inputs. By looking at the interface, you would not know what type of inputs that the implementing classes expect, thus making the interface basically useless. You could not interchange different concrete classes that all use the same interface, as they all expect different inputs (have different interfaces).

    I could not replace classA with classB with the assurance that they would both work, since they both have the same interface. This would basically make interfaces useless for every OOP pattern known to man.

    EDIT EXAMPLE

    class CommandList {
    
        public function addCommand(Command $command) {
            $this->commands[] = $command;
        }
    
        public function runCommands() {
            foreach ($this->commands as $command) $command->run($this);
        }
    }
    
    interface Command {
        public function run(CommandList $commandList);
    }
    
    class Hop implements Command {
        public function run(CommandList $commandList) {
            // hop here
        }
    }
    
    class Skip implements Command {
        public function run(CommandList $commandList) {
            // skip here
        }
    }
    

    See how the interface acts as a contract? If you break that contact, then things that implement Command would not be interchangeable.

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

Sidebar

Related Questions

In Interface Builder components I have Gradient Button I would like to create this
So in my project i would like have a nice treeview that has images.
I would like to have a QMainWindow that can change it's look at runtime,
I would like to have an .emacs setting so that tabs are always formed
I have a module that uses std::string as part of its interface. I would
I have this C# class structure that I would like to refactor to use
I would like to have a custom snippet of html form code that takes
I would like to design a web app that allows me to sort, browse,
I would like to have a Windows 2003 server fire a script to fire
I would like to have Ajax form in Rails so i'm using form_remote_tag. The

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.