Override exists to allow runtime binding of methods based on the object in use. This is tougher to see easily in PHP since its datatype weak, so we'll use an example with typehinting a function with an interface:
PHP Code:
interface IMyInterface
{
public function myMethod();
}
class Parent implements IMyInterface
{
// Parent is typeof IMyInterface
public function myMethod()
{
print __METHOD__;
}
}
class Child extends Parent
{
// Child is typeof Parent making it implicitly typeof IMyInterface
public function myMethod()
{
print __METHOD__;
}
}
function execMyMethod(IMyInterface $imiObj)
{
$imiObj->myMethod();
}
$p = new Parent();
$c = new Child();
execMyMethod($p); // Prints Parent::myMethod
execMyMethod($c); // Prints Child::myMethod
What this does is allows any child of Parent to dynamically override via erasure the myMethod to replace the desired functionality of Child into Parent. The execMyMethod would also work with a typeof Parent.
Final simply indicates last in chain allowed, and cannot be overridden from that point on. Can be applied from either a class or method level, including static methods:
PHP Code:
class Parent
{
public function myMethod(){}
}
class Child extends Parent
{
public final function myMethod(){}
}
class Child2 extends Child
{
public function myMethod(){} // Fatal error, cannot override final method declared in Child
}
final class Child3 extends Parent
{
}
class Child4 extends Child3 // Fatal error, cannot override final class Chiild3
{
}
Does that make sense?