Jump to content

Warning where methods are not compatible


NotionCommotion
Go to solution Solved by Jacques1,

Recommended Posts

How can I prevent getting a warning?  I can make Editor::display($page_id=NULL), but I would rather not as not passing a value should represent an error.

 

Strict Standards: Declaration of MyApp\Editor::display() should be compatible with MyApp\Base::display($page_id = NULL) in /var/www/application/classes/Editor.php on line 0

<?php
namespace MyApp;
class Base
{
    public function display($page_id=null){
        return ['menu_main'=>$this->getMenu()];
    }
}

class Editor extends Base
{
    public function display($page_id){
    }
}
Edited by NotionCommotion
Link to comment
Share on other sites

  • Solution

You can't override a method and then change the signature entirely. This wouldn't make any sense, because one of the core principles of OOP is that you can substitute superclasses with subclasses. You're trying to violate this principle, because $obj->display() would work for instances of Base but not for instances of Editor.

 

What you can do of course is make the parameter optional in both classes and throw an InvalidArgumentException when null is passed to the method of Editor.

 

Or try to set up a less confusing architecture (e. g. two different methods).

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.