6

PHP官方文档在解释类和对象部分下的扩展时,它说:

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

所以我想知道,什么是参数签名?

文档中的示例如下:

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

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

官方在线链接

4

1 回答 1

8

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

于 2013-04-25T16:24:18.597 回答