titreader Posted August 18, 2013 Share Posted August 18, 2013 According to the book when overriding a parent class must use the same number of parameters. Why is this? What happens when I pass one more parameter. I tested it by passing an extra parameter than the original (parent) signature and it works just fine so I failed to understand the idea behind having the same number of parameters. Can someone clarify this? Thanks Link to comment Share on other sites More sharing options...
Larry Posted August 19, 2013 Share Posted August 19, 2013 Sorry for the confusion. In other OOP languages, you can create multiple methods with the same name but a different number of parameters. The parent version of the method might take 2 parameters but the child version would take 3. Which version of the method is called will depend upon how many parameters are passed. This is known as overloading a method. PHP does not support the traditional version of overloading is the main point of what I'm trying to say. I believe PHP will throw an error if you intend to override a method but you actually overload it. I expect what you witnessed when passing extra arguments (which is different than defining it to accept a different number of parameters) is that PHP ignore the extra argument because there's no parameter for it. Link to comment Share on other sites More sharing options...
titreader Posted August 19, 2013 Author Share Posted August 19, 2013 It makes perfect sense now. Thanks for your time to clarify this and thanks for the great book you've written. Link to comment Share on other sites More sharing options...
Larry Posted August 19, 2013 Share Posted August 19, 2013 Sure thing. Thanks for the nice words! Link to comment Share on other sites More sharing options...
Guest Deleted Posted August 21, 2013 Share Posted August 21, 2013 This helps me. I was confused on what overloading was all about. Overriding made perfect sense, overloading did not. Link to comment Share on other sites More sharing options...
Antonio Conte Posted August 21, 2013 Share Posted August 21, 2013 Method overloading is such a beautiful tool, and I really wish PHP could implement it. Such a shame. Link to comment Share on other sites More sharing options...
Recommended Posts