Category Archives: OOPs

Interface

PHP does not support multiple inheritance directly, to implement this we need Interface.

Here method are declared in the Interface body, and the body part of the method is implemented in derived class. Variables are declared as constant and it can not be changed in the child classes.

We use implement keyword to extend this kind of class, at the same time we can implement more than one interface and one interface can be implemented by another interface.

All methods declared in an interface must be public and the variables should be constant.

This is mandatory that we must declare the body part of the method in the derived class otherwise an error message will be generated.

PRIMARY PURPOSES OF AN INTERFACE

  • Interfaces allow you to define/create a common structure for your classes – to set a standard for objects.
  • Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources.
  • Interfaces provide a flexible base/root structure that you don’t get with classes.
  • Interfaces are great when you have multiple coders working on a project – you can set up a loose structure for programmers to follow and let them worry about the details.

WHEN SHOULD YOU MAKE A CLASS AND WHEN SHOULD YOU MAKE AN INTEFACE?

  • If you have a class that is never directly instantiated in your program, this is a good candidate for an interface. In other words, if you are creating a class to only serve as the parent to other classes, it should probably be made into an interface.
  • When you know what methods a class should have but you are not sure what the details will be.
  • When you want to quickly map out the basic structures of your classes to serve as a template for others to follow – keeps the code-base predictable and consistent.

A. A class can not extend more than one interface.

B. A class can implement more than one interface.

C. An interface can extend more than one interface.

D. An interface can not implement more than one interface.

EXAMPLE

<?php
interface a{
public function test();
}

class b implements a{
public function test(){
echo “Function Test”;
}
}

$b=new b();
$b->test();
?>

Inheritance

Inheritance is the mechanism of deriving a new class from an existing class. It allows child class to access  the data member and functions of parent class.

To implement Inheritance  you should use the keyword ‘extends’ in the class definition .

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

 

Example

<?php
class a
{
public  function function_a(){
echo “class A”;
}
}

class b extends a
{
public function function_b(){
echo “class B”;
}
}

echo b::function_b();
echo b::function_a();

?>

PHP Support the below given inheritance

Single level
hierarchical level
Multilevel
Multiple
Hybrid

It does support multiple/Hybrid inheritance but multiple can be implemented through use of interface not directly