0

I'm working with a basic class library where the parent class has a function...

function functionName($param) {

}

Then, in a child class extending this class I have...

function functionName($param, $param2, $param3) {

}

When I make calls using this child class, I end up with the following PHP notice...

Declaration of child_class::functionName() should be compatible with that of parent_class::functionName()

The code still completes successfully, but the notice worries me and I'd like to get rid of it. I've found that if I add the additional parameters to the parent class's version of the function then the problem goes away. These parameters are not necessary anywhere except for the in the extended class, though, so it seems strange to have to add that...doesn't it?

Is there a better way to fix such an issue, or should I just make sure to add those same parameters in all instances of this function regardless of whether or not that particular class would actually need it?

Any information on this would be greatly appreciated. Thanks!

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Drew Angell
  • 25,968
  • 5
  • 32
  • 51
  • Do you need both available in the child class? – Kelly Copley Dec 11 '12 at 07:31
  • Php doesen't support overloading as far as I know, take a look at this topic http://stackoverflow.com/questions/2994758/what-is-function-overloading-and-overriding-in-php – v0d1ch Dec 11 '12 at 07:45
  • I can't seem to duplicate the problem.. I'm able to create 2 classes with a child class overriding a method from the parent class only having a different signature and both seem to work fine. Can you post the code for you classes in its entirety? – Kelly Copley Dec 11 '12 at 07:52
  • Depends on what you want. You could `function functionName($param, $param2 = null, $param3 = null)` To combine both. You can use [php override](http://php.net/manual/en/function.override-function.php) But the best in my opinion is to call methods different because they act different – Ron van der Heijden Dec 11 '12 at 08:04

2 Answers2

1

You say it works when you add additional parameters that's because in OOP Method Overriding does the following

"The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class"

The below Link might help you

Declaration of Methods should be Compatible with Parent Methods in PHP

Community
  • 1
  • 1
Vaibhav
  • 289
  • 1
  • 3
  • 11
  • I guess I'm not following..?? This seems to be stating what I thought would happen, and what does seem to be happening, but it's also throwing this notice at me..?? – Drew Angell Dec 13 '12 at 07:55
1

you need to have the exact same signatures if you're inheriting a class.

A quick workaround can be done using func_get_args:

function functionName ($param1) { // This is in the derived clas
    $params = func_get_args();
    $param2 = $params[1];
    $param3 = $params[2];
}
skyronic
  • 1,577
  • 1
  • 11
  • 15
  • I'm not following..?? What would $param1 have in it? – Drew Angell Dec 13 '12 at 07:57
  • @AndrewAngell - $param1 is assigned by default in the function. If you call $foo->functionName(1, 2, 3) - PHP internally calls $foo->functionName(1), and func_get_args() returns array(1, 2, 3) – skyronic Dec 13 '12 at 11:49