Let’s say I have something like this:
abstract class Parent
{
protected function foobar($data)
{
//do something with data
return $result;
}
}
class Child extends Parent
{
public function foobar()
{
$data = ...;
return parent::foobar($data);
}
}
As you can see, foobar is generically defined in Parent, and then Child passes class-specific data into the parent which returns the result. The reason this is done is because each child class of parent has its own data, but the method itself remains the same – only the parameter is different. The child also publicly exposes the method.
Is it a better idea to do something like this, where I create a method of the same name in both the parent and the child? Or should I maybe create a _foobar method in the Parent class and just have the Child‘s foobar method call that?
The The Liskov Substitution Principle states that:
Meaning that:
Functions that use pointers or references to base classes must be able to do use objects of derived classes without knowing it.
In your example you are violating that by changing the signature of the foobar method (overriding). foobar should have the same signature to the outside world, it can do something different with a related intent. If you need another function then name it something different, Your Child no longer becomes an isa of the Parent.