Tuesday, 31 May 2011

Lesson 6 : Object Oriented PHP

Retweet this button on every post blogger
PHP is object oriented programming language it includes in PHP since PHP4 and improved to full object model in PHP5 which provide visibilty, absract, and final classes and methods additional magic methods ,interfaces, cloning and typehinting.

PHP treats objects in the same way as references or handles, meaning that each variable contains an object reference rather than a copy of the entire object.


An example of simple class : - 
<html>
<head><title>Object oriented PHP </title></head>
<body>
<?php
class example 

{
   public $var1;
  public $var2;


   function getmultiply() 
  {
     $multi = $this->var1*$this->var2;
    return $multi;
  }


}
?>



<?php
$obj = new example(); ///creting class object.
$obj->var1 = 2;
$obj->var2 = 3;
echo $obj->getmultiply();
?>
</body>
</html>



output : - 
6


To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation.


access specifiers : -
there are basically three types of access specifier in PHP
1. public - data member and methods defined as public are available to use within and outside of the class.


2. protected - data member and methods defined as protected are available only to use in child classes .


3. private -  data member and methods defined as private are available only to use within the  class itself that defines it .


in the example above define class variables $var1 $var2 are public data member. 


Inheritence : -
inheritence is a greate concept of oop programming which gives reusability and allows child classes to use existing methods and data member of the parent class in child class.


parent class :- class from which we are inheriting methods and data mmeber. methods and data members must be defined as public or protected.


child class :- class in which we are  inheriting parent class .


for inherit a class within the child class we use a keyword called extends.


An example of inheritence :- 



<html>
<head><title>Object oriented PHP </title></head>
<body>
<?php
class example 
{
   public $var1;
  public $var2;

   function getmultiply() 
  {
     $multi = $this->var1*$this->var2;
    return $multi;
  }
}

class   add extends example
{
   function addme()
      $sum = $this->var1+$this->var2; // data member form example class.
     return $sum;
}
?>

$child = new  add()
$child->var1 = 2;
$child->var2 = 3;
echo  $child->
addme();


</body>
</html>
output :- 5

Abstract class :--
 An abstract is a class incomplete class which can not be instantiated , it is required for an class to be abstract that it must contain at least one method within it defined as abstract. An abstract method is nothing but only declaration  of the method and its implementation within the second class which inherit it. 
        
<html>
<head><title>Object oriented PHP </title></head>
<body>
<?php
abstract class example 
{
   public $var1;
  public $var2;

   function getmultiply() 
  {
     $multi = $this->var1*$this->var2;
    return $multi;
  }
abstract public function getdivide(); ///only declaration of 
}

class   add extends example
{
   function addme()
      $sum = $this->var1+$this->var2; // data member form example class.
     return $sum;

   function 
getdivide()


  {

      $divide  = $this->var1/$this->var2;

       return $divide;

  }

}
?>
<?php 
$child = new  add()
$child->var1 = 2;
$child->var2 = 3;
echo  $child->
addme();

echo "<br/>";

echo $child->
getdivide();

?>


</body>
</html>








No comments:

Post a Comment