Sunday, December 16, 2012

PHP Abstract Class

In this tutorial we will study about abstract class in PHP, how to declare, use an abstract class etc. Examples will help you to understand the concepts of abstract class more precisely.
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:
<?php
abstract class One{
public function disp(){
echo "Inside the parent class<br/>";
}
}
class Two extends One{
public function disp(){
echo "Inside the child class<br/>";
}
}
class Three extends One{
//no method is declared
}
$two=new Two();
echo "<b>Calling from the child class Two:</b><br/>";
$two->disp();
echo "<b>Calling from the child class Three:</b><br/>";
$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
Example:
<?php
abstract class One{
abstract function disp();
}
class Two extends One{
public function disp(){
echo "Inside the child class<br/>";
}
}
class Three extends One{
public function disp(){
echo "Inside the child class 2<br/>";}
}
$two=new Two();
echo "<b>Calling from the child class Two:</b><br/>";
$two->disp();
echo "<b>Calling from the child class Three:</b><br/>";
$three=new Three();
$three->disp();
?>
Output:
Calling from the child class Two:
Inside the child class
Calling from the child class Three:
Inside the child class 2

PHP Inheritance Class

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
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:
<?php
class One
{
public function printItem($string)
{
echo 'This argument is passing from '.$string.'<br/>';
}
}
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 Create Instance

In the current tutorial we will study how to instantiate an object in PHP. Example will help you to learn it precisely. You will also come to know about the use of new, -> operator.
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:
<?php
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 "<br/>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

PHP Class Constant

In this tutorial we will study how to declare a class constant in PHP. 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.
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:
<?php
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

PHP Constructor and Destructor

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:
<?php
class ParentClass{
function __construct(){
print "In parent class <br/>";}}
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:
<?php
class ParentClass{
function __construct(){
print "In parent class <br/>";
}
function __destruct(){
print "Parent Class Destructor called<br/>";
}
}
class ChildClass extends ParentClass{
function __construct(){
parent::__construct();
print "In child class<br/>";
}
function __destruct(){
print "Child Class Destructor called<br/>";
}
}
$obj1=new ParentClass();
$obj2=new ChildClass();
?>
Output:
In parent class
In parent class
In child class
Child Class Destructor called
Parent Class Destructor called

OOP In PHP

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:
Class
Object
Polymorphism
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.
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:
<?php
class A
{
public function disp(){
echo "Inside the class<br/>";
}}
$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.