Custom Search

Thursday, February 28, 2013

OOPS CONCEPT IN PHP


PHP OOP Concepts

Object Oriented Programming  is a paradigm which is nowadays the most popular way to develop any application and most of the modern day language is based on this paradigm.
OOP or Object Oriented Programming PHP has so many features like:
  1. Class
  2.  Object
  3. Polymorphism
  4. Dynamic Binding...etc.
PHP Class Object:

In object oriented programming a class can be an abstract data type, blue print or template. You could consider the name of a class as noun like name of a person, place or thing.  For example Fruit is a class, where apple, orange are the object of this class.
Object is the instantiate of a class. A class consists of a member variable and methods. In PHP we need to declare the access specifiers (public, private, or protected) separately for
PHP Object Class Example:
class A
{
public function disp(){
echo "Inside the class
"
;
}}
$a=new A();
$a->disp();
A::disp();
?>
Output:
Inside the class
Inside the class

Explanation:

In the above programming we create a class named A, it has a publically declared function disp(), on the outer side of the class we instantiate the object $a, we  call the function disp with -> operator. Any class can access it's member using :: operator. 

PHP Constructor & Destructor:

Like other OOP based languages PHP also supports constructor method for classes. As any other language's constructor method, in PHP constructor method is called for every object creation.
We can call parent's constructor method using parent::__construct() from the child constructor.
PHP also supports destructor like C++. This method is called as soon as the references of the object are removed or if we destroy the object . This feature has been included in PHP 5. Like constructor method we can call the destructor method of parent class by parent::__destruct().
The destructor is being called any how, means even after executing the exit() code destructor method could be called.

PHP Constructor & Destructor Example:

class ParentClass{
function __construct(){
print "In parent class
"
;}}
class ChildClass extends ParentClass{
function __construct(){
parent::__construct();
print "In child class";}}
$obj1=new ParentClass(12);
$obj2=new ChildClass();
?>

Output:
In parent class
In parent class
IIn child class

Example:

class ParentClass{
function __construct(){
print "In parent class
"
;
}
function __destruct(){
print "Parent Class Destructor called
"
;
}
}
class ChildClass extends ParentClass{
function __construct(){
parent::__construct();
print "In child class
"
;
}
function __destruct(){
print "Child Class Destructor called
"
;
}
}
$obj1=new ParentClass();
$obj2=new ChildClass();
?>

Output:
In parent class
In parent class
In child class
Child Class Destructor called
Parent Class Destructor called

PHP Class Constants:

Sometimes we need to have constant values  in our program which remains same through and through. We do not need to put a $ sign before the constant to use or to declare it.
The value of the constant should remain unchanged that means it must be a constant expression.

Example:

class A
{
const newValue="Constant value does not consist $ sign";
function display()
{
echo self::newValue."\n";
}
}
$a=new A();
$a->display();
?>

Output:
Constant value does not consist $ sign

Explanation:

In the above example there is a single class declaration named A. Outside of this class
Create an Instance:
In OOPs programming we need to instantiate each object. In the following example we create two classes A and B. To access variables of the class we should use self keyword following with double colon sign.
When we need to access the variables of a class from an object we use -> operator sign. To instantiate an object we need to use new operator as:
object-name=new class-name();
Following example is based on these concept, go through the example and run it on your computer, you will come to know how it works:

How to Create PHP Instance with Example:

class A
{
public static $one;
public function set($one){
self::$one=$one;
}
public function get(){
echo "Value of variable is:".self::$one;}
}
class B
{
public function disp(){
echo "
Inside Class B"
;}
}
$a=new A();
$a->set(12);
$a->get();
$class='B';
$a=new $class();
$a->disp();
?>

Output:
Value of variable is:12
Inside Class B
Inheritance in PHP:

Inheritance is a property of Object Oriented Programming, PHP also supports this principle in its programming as object model. With the help of this property classes and objects  get more flexibility, scalability in programming
At the same time we must keep in our mind that when we should not use the inheritance. We must not use inheritance when we have only different values in classes, we must use the inheritance when the structure and the behavior are different. It is always advisable that we must give preference to object composition over inheritance.
 In the following example we will come to know how a class could be declared as a child class of another class using extends keyword.

PHP Inheritance Example:

class One
{
public function printItem($string)
{
echo 'This argument is passing from '.$string.'
'
;
}
}
class Two extends One
{
}
$baseObj=new One();
$childObj=new Two();
$baseObj->printItem("Base");
$childObj->printItem("Child");
?>

Output:
This argument is passing from Base
This argument is passing from Child

PHP Mutator and Accessor Methods:

In object-oriented programming, the mutator method (also called "setter") is used to initialize the member variables of a class. According to the encapsulation principle the variables of the class are declared as private to protect and can be modified by a public declared function.
On the other hand accessor methods are used to get the values of the private data member.
These methods can be implemented on non-object-oriented programming language as well. In this case we have to pass the address (reference) of the variable.

PHP Mutator Method Example:

class One{
private $string;
public function mutator($arg){
$this->string=$arg;}
public function accessor(){
echo "Value is: ".$this->string;}}
$one=new One();
$one->mutator("roseindia");
$one->accessor();
?>

Output:
Value is: roseindia

PHP Autoloading Classes:

It is very common in PHP programming that we need to write long listing of files which has to be include in a single file. It is indeed a hectic job for each PHP programmer.
But with the advent of PHP 5.0, this is not necessary anymore. We can define an   _autoload function to call automatically the class/interface which has not been defined yet.

PHP Autoload Class Example:

function _autoload($classname){
require_once $classname.'.php';
}
$obj=new mysqli();
?>

PHP Static Methods and Variables:

Static keyword is used to make only one copy of the variable for the class. All objects of that class will access one copy of that attribute. Static data are used when some data has to share within the all objects of that class.
If we declare a method as static then we can access the method using class, without instantiating any object.

Example of PHP Static Variables & Methods :

class One{
private static $var=0;
function __construct(){
echo "Inside constructor";
self::$var++;
print self::$var;}
static function disp(){
print self::$var;}}
One::disp();
?>

Output:
0

PHP Abstract Class:

  • Abstract classes and methods are introduced in PHP 5. The concept behind the abstract class is that we need to extend this class by its descendant class(es).
  •  If a class contains abstract method then the class must be declared as abstract. Any method which is declared as abstract must not have the implementation part but the declaration part only.
  • The child classes which inherits the property of abstract base class, must define all the methods declared as abstract.


PHP Abstract Class Example:

abstract class One{
public function disp(){
echo "Inside the parent class
"
;
}
}
class Two extends One{
public function disp(){
echo "Inside the child class
"
;
}
}
class Three extends One{
//no method is declared
}
$two=new Two();
echo "Calling from the child class Two:
"
;
$two->disp();
echo "Calling from the child class Three:
"
;
$three=new Three();
$three->disp(); 
?>

Output:
Calling from the child class Two:
Inside the child class
Calling from the child class Three:
Inside the parent class

PHP Interface Class:

  • PHP does not support multiple inheritance directly, to implement this we need Interface. It is much similar to Interface of Java.
  • In PHP, signature of the 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.

Example:

interface Inter{
const a="This is constant value";
public function disp(); }
class implements Inter{
function show(){
echo self::a."
"
;}
public function disp(){
echo "Inside the disp function";}}
$a=new A();
$a->show();
$a->disp();
?>

Output:
This is constant value
Inside the disp function

PHP Polymorphism Function:

The PHP Polymorphism Method is one of the feature of OOP language. Generally we get polymorphism in two ways:
  • Compile time
  • Run time
Compile time polymorphism PHP is like function overloading, operator overloading etc. In Polymorphism function overloading we can create more than one function with same name but with different signature that means it could have different number of parameters, different datatype of the parameter etc. Depending on the actual number and/or the data type the compiler resolve the actual call .
In operator overloading predefined operators treat as functions and one object calls this function and passes another object as argument. PHP neither supports operator overloading nor function overloading.
Inheritance and virtual functions are the examples of PHP Run time polymorphism. PHP supports Inheritance as well as virtual function as function overriding.
Following examples will exemplify each of the types of polymorphism:

PHP Polymorphism Function Example:

class A{
function Disp(){
echo "Inside the Base class
"
;}}
class extends A{
function Disp(){
echo "Inside the Chlid class
"
;}}
class extends A{
}
$obj=new B();
$obj->Disp();
$obj2=new C();
$obj2->Disp();
?>

Output:
Inside the Chlid class
Inside the Base class

PHP Object Iteration:

In PHP 5 a new way for objects is introduced to iterate through a list of items. In this technique we generally use foreach statement. All visible (public, protected) properties are used in the iteration. To display the private properties we can use any method which would display these all.

PHP Object Iteration Example:
class One{
public $a="A";
public $b="B";
public $c="C";
protected $pro="Protected Data";
private $pri="Private Data";
function iteration(){
echo "One::iteration
"
;
foreach($this as $key=>$value){
print "$key=>$value
"
;}}
}
$one=new One();
echo "If we call the iteration method:
"
;
$one->iteration();
echo "If we iterate the object:
"
;
foreach($one as $key=>$value){
print "$key=>$value
"
;
}
?>
Output:

If we call the iteration method:

One::iteration
a=>A
b=>B
c=>C
pro=>Protected Data
pri=>Private Data
If we iterate the object:
a=>A
b=>B
c=>C



No comments:

Post a Comment