NotionCommotion Posted September 11, 2016 Share Posted September 11, 2016 (edited) 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 September 11, 2016 by NotionCommotion Quote Link to comment https://forums.phpfreaks.com/topic/302148-warning-where-methods-are-not-compatible/ Share on other sites More sharing options...
Solution Jacques1 Posted September 11, 2016 Solution Share Posted September 11, 2016 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). 1 Quote Link to comment https://forums.phpfreaks.com/topic/302148-warning-where-methods-are-not-compatible/#findComment-1537371 Share on other sites More sharing options...
NotionCommotion Posted September 11, 2016 Author Share Posted September 11, 2016 Thanks Jacques, Nice explanation. Quote Link to comment https://forums.phpfreaks.com/topic/302148-warning-where-methods-are-not-compatible/#findComment-1537380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.