1. www.vineetsaini.wordpress.com
Abstract Class & Interface in PHP
Abstract Class
Abstract classes are those classes which can not be directly initialized. Or in other word we
can say that you can’t create object of abstract classes.
Abstract classes always created for inheritance purpose. You can only inherit abstract class
in your child class. Many peoples say that in abstract class at least your one method should
be abstract. Abstract method are the method which is only defined but declared. But your
any class has at least one method abstract than your class is abstract class.
Abstract class are also known as base class. We call it base class because abstract class are
not the class which is available directly for creating object. It can only act as parent class of
any normal class. You can use abstract class in class hierarchy. It means one abstract class
can inherit another abstract class also.
We can create abstract classes in php using abstract keyword. Once we will make any class
abstract in php we can’t create object of that class.
1. An abstract method contains no body.
2. An abstract method is not implemented by the base class.
3. There can be no objects of an abstract class.
4. An abstract class can inherit from a class and one or more interfaces.
5. An abstract class can implement code with non-Abstract methods.
6. An abstract class can have constructors or destructor's.
7. An abstract class cannot support multiple inheritance.
<?php
abstract class testParent
{
public function abc()
{
echo 'Vineet';
}
}
class testChild extends testParent
{
public function xyz()
{
echo 'Saini';
}
}
$a = new testChild();
echo $a->abc();
echo $a->xyz();
?>
2. www.vineetsaini.wordpress.com
Interface
We know that PHP does not support the multiple inheritance. For support multiple
inheritance in php we use Interface.
Interface is just like a class using interface keyword and contains only function declarations
(function with no body). Function is defined in the classes where you call it.
Interface gets useful when you need to extend a class from more than one parent class
since class can’t do this.
An interface cannot contain any functionality. It only contains definitions of the methods
<?php
interface template1
{
public function f1();
}
class test implements template1
{
public function f1()
{
echo 'Vineet';
}
public function f2()
{
echo 'Saini';
}
}
$a = new test();
echo $a->f1();
echo $a->f2();
?>
Differences between abstract class and interface in PHP
Following are some main difference between abstract classes and interface in php.
1. In abstract classes this is not necessary that every method should be abstract. But in
interface every method is abstract.
2. Multiple and multilevel both type of inheritance is possible in interface. But single
and multilevel inheritance is possible in abstract classes.
3. Method of php interface must be public only. Method in abstract class in php could
be public or protected both.
4. In abstract class you can define as well as declare methods. But in interface you can
only defined your methods.