8

The PHP official documentation while explaining about extends under classes and objects section, it says:

"When overriding methods, the parameter signature should remain the same or PHP
will generate an E_STRICT level error. This does not apply to the constructor
which allows overriding with different parameters."

So I want to know, what a parameter signature is?

The example inside the documentation is the following:

<?php
class ExtendClass extends SimpleClass
{
    // Redefine the parent method
    function displayVar()
    {
        echo "Extending class\n";
        parent::displayVar();
    }
}

$extended = new ExtendClass();
$extended->displayVar();
?> 

Official online link

Havelock
  • 6,913
  • 4
  • 34
  • 42
Yousuf Memon
  • 4,638
  • 12
  • 41
  • 57

1 Answers1

9

The parameter signature is simply the definition of parameters in the definition (signature) of a method. What is meant with the quoted text is, to use the same number (and type, which is not applicable in PHP) of parameter when overriding a method of a parent class.
A signature of a function/method is also referred to as a head. It contains the name and the parameters. The actual code of the function is called body.

function foo($arg1, $arg2) // signature
{
    // body
}

So for example if you have a method foo($arg1, $arg2) in a parent class, you can't override it in a extended class by defining a method foo($arg).

Havelock
  • 6,913
  • 4
  • 34
  • 42
  • 1
    doesn't matter what the doc says, it can be overridden exactly like that bro. Try it out. – raidenace Apr 25 '13 at 16:32
  • @Raidenace then taht would be a different method and not an extension of the one in the parent class. – Havelock Apr 25 '13 at 16:39
  • No. If you have a parent class with `function displayVar()`, and child class which has `function displayVar($x)` then calling `childObj->displayVar()` should call the parents `displayVar()` with no params, since according to you that is a different method. But that is not how PHP behaves. – raidenace Apr 25 '13 at 16:44
  • 1
    @Raidenace, no it will trigger an error `Declaration of ChildClass::displayVar() should be compatible with that of SimpleClass::displayVar()` – Havelock Apr 25 '13 at 16:54
  • 1
    did you try it? And also it should not trigger that error since, they are two signatures and hence two different functions, no? – raidenace Apr 25 '13 at 16:56
  • I did. The question here is, did you? – Havelock Apr 25 '13 at 16:57
  • Yup. Did it too. Maybe you should paste the code you are trying out? – raidenace Apr 25 '13 at 16:58
  • I tried that example from viper-7 (still accessible trough archive.org) and it does not produce any warnings works just fine despite documentation, i have PHP v5.6.26. Is documentation out of date for this matter or its a bug? – Roman Toasov Oct 28 '16 at 00:07