0% found this document useful (0 votes)
58 views

Interface: Shubhangi Shinde

The document discusses interfaces in PHP. It states that interfaces allow classes to specify methods without defining how they are handled. Classes that implement interfaces must declare the methods listed in the interface. Interfaces can be used for type hinting. The document also compares interfaces to abstract classes and discusses differences such as interfaces requiring all methods to be public and abstract. It provides an example of a Countable interface and class. Finally, it discusses final classes and methods that cannot be inherited or overridden.

Uploaded by

shubhangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Interface: Shubhangi Shinde

The document discusses interfaces in PHP. It states that interfaces allow classes to specify methods without defining how they are handled. Classes that implement interfaces must declare the methods listed in the interface. Interfaces can be used for type hinting. The document also compares interfaces to abstract classes and discusses differences such as interfaces requiring all methods to be public and abstract. It provides an example of a Countable interface and class. Finally, it discusses final classes and methods that cannot be inherited or overridden.

Uploaded by

shubhangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Interface

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.

Interfaces are defined using theinterfacekeyword, in


the same way as a standard class, but without any of
the methods having their contents defined.
All methods declared in an interface must be public;
this is the nature of an interface.
To implement an interface, theimplementsoperator is
used.
All methods in the interface must be implemented
within a class; failure to do so will result in a fatal error.
Classes may implement more than one interface if
desired by separating each interface with a comma.

interface Countable
{
public function count();
}
class MyThings impmplements Countable
{
public function count();
{
return count($this->arrayofThings);
}
}

Interface example

Differencesbetween abstract class and interface in


PHP
In abstract classes this is not necessary that every
method should be abstract. But in interface every
method is abstract.
Multiple andmultilevelboth type of inheritanceis
possible in interface. But single and
multilevelinheritanceis possible in abstract classes.
Method of php interface must be public only. Method
in abstract class in php could be public or protected
both.
In abstract class you can define as well as declare
methods. But in interface you can only define your
methods.

Final classes and methods


Final classes :
In some cases you may want to prevent a class
from being inherited from or a function to be
overridden.
This can be done with the final keyword, which
simply causes PHP to throw an error if anyone
tries to extend your final class or override your
final function.
A final class is a class that cannot be extended.
To declare a class as final, you need to prefix
the class keyword with final.

final class BaseClass


{ public function myMethod()
{
echo "BaseClass method called";
} } //this will cause Compile error
class DerivedClass extends BaseClass
{ public function myMethod()
{ echo "DerivedClass method called"; }
}
$c = new DerivedClass(); $c->myMethod();

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();

When to declare a class as final


You should declare a class as final
when you think that you
implementation of that class should
not change in the derived class.
You should do this mainly for Utility
classes where you dont want the
behavior /implementation of your
class to change.

When to declare a method as final


You should declare a class method as
final when you think that the method
you develop contains necessary
functionality to support your
application and any modification or
change to the functionality can cause
unexpected errors/bugs.

You might also like