Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a parameter signature in PHP?

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

like image 202
Yousuf Memon Avatar asked Apr 25 '13 16:04

Yousuf Memon


People also ask

What are parameters in PHP?

PHP Parameterized functions They are declared inside the brackets, after the function name. A parameter is a value you pass to a function or strategy. It can be a few value put away in a variable, or a literal value you pass on the fly. They are moreover known as arguments.

What is a signature of a function?

A function signature (or type signature, or method signature) defines input and output of functions or methods. A signature can include: parameters and their types. a return value and type. exceptions that might be thrown or passed back.

What is parameter and argument in PHP?

Function Parameters or Arguments These parameters are used to accept inputs during runtime. While passing the values like during a function call, they are called arguments. An argument is a value passed to a function and a parameter is used to hold those arguments.

What are signatures in code?

Code signing is the process of digitally signing executables and scripts to confirm the software author and guarantee that the code has not been altered or corrupted since it was signed. The process employs the use of a cryptographic hash to validate authenticity and integrity.


1 Answers

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).

like image 158
Havelock Avatar answered Sep 19 '22 14:09

Havelock