为什么子方法必须与父方法具有相同的参数?

I have this code:

abstract class Base{

   public function delete(){
     // Something like this (id is setted in constructor)
     $this->db->delete($this->id);
   }

}

Then i Have another class which extends Base, for instance:

class Subtitles extends Base{

    public function delete($parameter){
         parent::delete();
         // Do some more deleting in transaction using $parameter
    }

}

which also happens to have method delete.

Here comes the problem:

When i call

$subtitles->delete($parameter)

I get the:

Strict error - Declaration of Subtitles::delete() should be compatible with Base::delete() 

So my question is, why i can't have the method of descendant with different parameters?

Thank you for explaining.

This is because PHP does method overriding not method overloading. So method signatures must match exactly.

As a work arround for your issue you can restructure delete on your base class to

public function delete($id = null){
  // Something like this (id is setted in constructor)
  if ($id === null) $id = $this->id;
  $this->db->delete($id);
}

Then change your subclasses method signature to match.

To override the function in the base class, a method must have the identical "Signature" to the one it is displacing.

A signature consists of the name, the parameters (and parameter order), and the return type.

This is the essence of polymorphism, and is where object-oriented programming gains much of its power. If you don't need to override the parent's methods, give your new method a different name.

This is suposed to be a comment to @orangePill's ansert, but I don't have reputation enough to comment.

I had this same issue with static methods, and I did the following, using late static binding. Perhaps it helps someone.

abstract class baseClass {
    //protected since it makes no sense to call baseClass::method
    protected static function method($parameter1) {
        $parameter2 = static::getParameter2();

        return $parameter1.' '.$parameter2;
    }
}

class myFirstClass extends baseClass {
    //static value, could be a constant
    private static $parameter2 = 'some value';

    public static function getParameter2() {
        return self::$parameter2;
    }

    public static function method($parameter1) {
        return parent::method($parameter1);
    }
}

class mySecondClass extends baseClass {
    private static $parameter2 = 'some other value';

    public static function getParameter2() {
        return self::$parameter2;
    }

    public static function method($parameter1) {
        return parent::method($parameter1);
    }
}

Usage

echo myFirstClass::method('This uses'); // 'This uses some value'

echo mySecondClass::method('And this uses'); // 'And this uses some other value'