Jump to content
Larry Ullman's Book Forums

Recommended Posts

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

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

Guest Deleted

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

 Share

×
×
  • Create New...