Monday 12 March 2012

PHP with OOPS


Ø       Characteristic of oops

·         Abstraction

Abstraction means to hide. Abstraction is used when only the required information is to be shown to the user and other common information or less useful information is to be hide from the user.

For example: When we go to purchase a car, the salesman does not give information like it has windows, doors, color of the car etc. But he gives information like what would be the average fuel consumption, whether the car has a AC inside or not, what will be the warranty, and any such facility which makes the car unique. Thus only required information is given to the user.

In Php, while declaring a variable, we are not showing to the user what is the data type but we are concerned with the value. So the common properties like the data type of variable are hidden and only the required information which has the value of variable is given.

·         Encapsulation

Encapsulation means to hide. But Encapsulation hides the structure of the object. It means the working process is not shown to the user.

For example: While using ATM when you ask for some amount, the whole transaction process like how the amount came, how the amount is deducted from your savings account is not shown to the user. So to hide the process from the user is known as encapsulation.

In Php, when an addition of two values is done, the process how the value is added is not shown to the user but only the output is given.
         
·         Inheritance

Inheritance means hereditary. Inheritance refers to the concept of the parent child. Parent class is referred as the base class and the child class is referred as the derived class. The derived class derives the base class i.e. the child class derives the parent class.

So the child class can access the functionality i.e. methods, functions and variables of the parent class. So Inheritance means the derived class inherits the base class.

For example: the parent child concept. The child can use all the functions of the parents. A child may have the same characteristics as his/her parents. So he/she inherits the parents. This is Inheritance.

·         Polymorphism

The word Poly means many. So polymorphism means one name many works. It means that the thing is one but that one thing has many different functions.

For example: Suppose a person is one but he has many roles. A person may be student in the class, at home he/she will be a child, a sister for a brother. So the person is one but has many roles i.e. polymorphism.

For example the operator ‘+’ is used for joining tow strings when used with string type values and is also used for addition of two integer type values. If we write “Hello” + “World” it will join both the strings as Hello World. If we write 5 + 5 it will give 10 by adding both the values. So this is one name many works that is polymorphism

Ø       Class

·         A class is a concept of object oriented programming language. A class contain methods, function and properties.

Syntax

class classname
{
Variable declaration
Funation declaration
}

  • A class definition always begins with the keywords “class”, followed by an identifier that represents the name of the class.
  • This are followed by a pair of curly braces ({}) that contains the member of the class.

Example

<?php
class test
          {
                   var $number;
                   function set($n)
                   {
                             $this->number = $n;
                   }
                   function get()
                   {
                             echo "Number Is ".$this->number;
                   }
          }
          $obj = new test();
          $obj->set(10);
          $obj->get();
?>

Output

               Number is 10

Ø       Constructor

  • A Constructor is a special type of subroutine which is called at the creation of the object of the class.
  • The task of Constructor is memory initialization of the variables that are declared in the class. It can accept parameters which are passed when the object of the class is created.

Syntax

function __construct([variable])
{
Variable initialization
}

OR

function classname([variable])
{
Variable initialization
}

Example

<?php
          class test
          {
                   var $name;
                   function __construct($nm)
                   {
                             $this->name = $nm;
                   }
                   function display()
                   {
                             echo "Name : ".$this->name;
                   }
          }
          $obj = new test("hello");
          $obj->display();
?>

output

               Name : hello

Ø       Destructor

  • Destructor is a method that is used to release the memory of the variables that were initialized.
  • The destructor method will be called automatically when all the references to a particular object are removed or when the object is explicitly destroyed.

Syntax:

                   function __destruct()
                   {
                             Release variable space from the memory
                   }

          Example:

                   <?php
                             class main
                             {
                                      var $name;
                                      function main ($nm)
                                      {
                                                $this->name=$nm;
                                                echo “Initialized”;
                                      }       
                                      function display ()
                                      {
                                                echo $this->name;
                                      }
                                      function __destruct ()
                                      {
                                                echo “Released”;
                                      }
                             }
                             $obj= new main (“Hello World”);
                             $obj->display ();
                   ?>

          Output:

                   Initialized
                   Hello World
                   Released

Ø       Inheritance

·         Inheritance means hereditary. Inheritance refers to the concept of the parent child. Parent class is referred as the base class and the child class is referred as the derived class. The derived class derives the base class i.e. the child class derives the parent class.
·         So the child class can access the functionality i.e. methods, functions and variables of the parent class. So Inheritance means the derived class inherits the base class.
·         For example: the parent child concept. The child can use all the functions of the parents. A child may have the same characteristics as his/her parents. So he/she inherits the parents. This is Inheritance.

Syntax for derived class:

Class derived classname extends base classname
{
          Variable declaration
          Function declaration
}

When a class is derived the object is created of the derived class and not of the base class. But object of the derived class can access the functions and variables of the base class.

Example

<?php
          class test
          {
                   var $number;
                   function set($n)
                   {
                             $this->number = $n;
                   }
                   function get()
                   {
                             echo "Number Is ".$this->number;
                   }
          }
          class test1 extends test
          {
                   function __construct()
                   {
                             echo "Derived Class<br/>";
                   }
          }
          $obj = new test1();
          $obj->set(10);
          $obj->get();
?>

Output

               Derived Class
               Number is 10

Ø       Method Overriding

  • There may be a situation when the base class and the derived class have a method with common name. Now as the object is created of derived class when the method with the same name is called using the object it will call the derived class method and not the base class method.
  • But if the user wants to access the base class method having the same name the method should be override.
  • To override a method the symbol :: (scope resolution) is used along with the function name and the class name.

Syntax:

Base classname :: function name ();

Example:

<?php
          Class main
          {
                   var $name;
                   function set ($nm)
                   {
                             $this->name=$nm;
                   }
                   function display ()
                   {
                             echo “In main class”;
                             echo $this->name;
                   }
          }
          Class main1 extends main
          {
                   var $nm;
                   function set1 ($name)
                   {
                             $this->nm= $name;
                   }


                   function display ()
                   {
                             echo “In derived class”;
                             echo $this->nm;
                             main :: display ();
                   }
          }
          $obj = new main1 (); //creating object of the derived class
          $obj->set (“Hello”); //calling base class set method
          $obj->set1 (“World”); //calling derived class set1 method
          $obj->display(); //calling display method of the derived class
?>

          Output:

                   In derived class
                   World
                   In Main class
                    Hello

Ø       Serialize Object

The serialize object is used to store a value in byte-stream representation. The serialize object uses serialize () function which returns a string containing the byte-stream representation of the value that can be stored. This function will save all variables in an object.

The other function that is used is unserialize () function which is used to recreate the original values.

          Example:

                   <?Php
                             Class student
          {
                   var $name;
                   var $age;
                   function student($nm="unnamed", $a=0)
                   {
                             $this->name = $nm;
                             $this->age = $a;
                   }
                   function displayage()
                   {
                             return $this->age;
                   }
                   function displayname()
                   {
                             return $this->name;
                   }
          }
          $obj = new student("ABC", 10);
          $data = serialize($obj);
          $value = unserialize($data);
          $age = $value->displayage();
          $name = $value->displayname();
          echo $name."<br/>";
          echo $age;
                   ?>

                        Output:

                             abc
                             10

Posted By : Hemangi Zala

No comments:

Post a Comment