Imagine i have 2 classes (i’ve simplified the logic here):
class Table {
public function addRow (Row $row){
$this->row = $row;
}
// lots of code
}
class Row {
// lots of code
}
And i want to extend the table class to do something similar so i create 2 new classes:
class SpecialTable extends Table{
public function addRow (SpecialRow $row){
parent::addRow($row);
}
// lots of code
}
class SpecialRow extends Row{
// lots of code
}
When i try to add a SpecialRow to a SpecialTable object i get a warning similar to:
PHP Strict standards: Declaration of SpecialTable::addRow() should be compatible with that of Table::addRow() in /SpecialTable.php on line XX
Can someone help me here? Is this bad practice and i should code it differently? Or is it just a warning that i should ignore?
Many thanks for any suggestions / guidance.
You should probably use a name like SpecialTable::addSpecialRow() if you’re changing the argument type – otherwise SpecialTable doesn’t actually extend Table, it overloads it (which isn’t supported in PHP).
Based on your simplified example, it should be
public function addRow(Row $row)since you just call the parent. Depending on what you’re actually doing to $row in that function, you could type-hintRowand check whether it’s aSpecialRowin code, or just use Row if you don’t need itsSpecialness.