Interface: Shubhangi Shinde
Interface: Shubhangi Shinde
Shubhangi Shinde
Interfaces
Object interfaces allow you to create code which
specifies which methods a class must implement,
without having to define how these methods are
handled.
Byimplementinginterface you are forcing any class
to must declaring some specific set of methods in
oop
An interface simply lists (function prototype) the
methods a class must implement.
However, in PHP a class can implement multiple
interfaces and these interfaces can also be used in
type hinting.
interface Countable
{
public function count();
}
class MyThings impmplements Countable
{
public function count();
{
return count($this->arrayofThings);
}
}
Interface example
Final Method
A final method is a method that cannot be
overridden. To declare a method as final,
you need to prefix the function name with
the final keyword.
A normal method (public or protected) can
be overridden in the child class. If you
want a method to remain fixed and
unchanging, prefix the definition with
thefinalkeyword.
A final method cannot be overridden.
class BaseClass
{
final public function myMethod()
{
echo "BaseClass method called";
}}
class DerivedClass extends BaseClass
{ //this will cause Compile error
public function myMethod()
{ echo "DerivedClass method called"; }
}
$c = new DerivedClass();
$c->myMethod();