0

I understand why PHP gives out this warning. What I don't understand, is why is this warning useful for anyone?

I mean, I find it completely valid, that an extended class, given that it can have new functionalities, could extend a function with new parameters, that are irrelevant in the extendees case.

Why is PHP trying to enforce(or guide, because in reality, this is just a warning, not an error) us, to define extended functions with exactly the same number and/or type of parameters?

Let's take a concrete example:

 class DatabaseTable {
      protected $TableName; // stores the table name
      protected $PrimaryKey; // stores the primary key column of this table
      protected $PrimaryKeyValue; // stores the exact primary key for the table row this instance points to
      protected $Structure; // stores the columns of this table, but in case of self, it is empty, as this would only be a template

      public function update() {
         // update the table row this instance points to, with anything that is in the structure
      }
 }

 class ConcreteTable extends DatabaseTable {
      protected $TableName = 'ConcreteTable';
      protected $PrimaryKey = 'ConcreteTablePrimaryKey';
      protected $Structure = array(
          'Column1' => 'data-in-column-1',
          'Column2' => 'data-in-column-2'
      );

      /** In this table, I am well aware that two columns exist, Column1, and Column2 **/
      public function update(bool $skipColumn1, bool $skipColumn2) {
         // revert skipped columns to their original value
         parent::update();
      }
Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • That's __not valid__ that `ConcreteTable::update`'s signature differs from parent one. – u_mulder Feb 07 '20 at 07:14
  • 2
    Because you can replace one sub-class with another and still keep same function calls. If your DatabaseTable would be Interface, then you could call any extended class with only checking Interface and not looking into class code. – Justinas Feb 07 '20 at 07:14
  • 1
    Hope the dupe is useful, if not, just ask for the question to be re-opened with an explanation of why it doesn't answer your question. – Nigel Ren Feb 07 '20 at 07:15
  • @NigelRen Yep, that helped, sry, I didn't found that post before asking my question. – Adam Baranyai Feb 07 '20 at 07:21

0 Answers0