Object Oriented Programming In PHP

0op’s In php:

  1. PHP 5.0 supporting number of object oriented programming concept like inheritance,interface, etc.
  2. oops concept we can sue to increase security,re-usability and application performance.
  3. According to oops every statement we should place within the class.
  4. class is the collection of members ,class contains  members of  properties and methods.
  5. variables deceleration within the class we can call as property, function deceleration within the class we can call as method.

Object:

  • object is the instance of class(memory location of class) we can access class member with the help of object

New:

  • using this key word we can allocate new memory location to load the class constants.
Example for New Key Word
<?php
class cls
{
var $x=100;
function fun()
{
echo “Hi”;
}
}
$obj = new cls;
$obj->fun();
echo $obj->x;
?>

Constructor:

  • It is type of method contains class name as method name.
  • By default every class contains a constructors use to load class member into memory location.
  • we can also create external constructor to execute some statement at time of creating object to the class.
  • In php we can create constructor in two ways.
               1.Using class name 2.using __construct key word. 

Destructor:

  • It is type of method use at time of destroying.
  • If you want to execute some statement just before destruction of object we can use destructor.
  • By using __destroy key word we can create  destructor in php program.
Example for constructor and destructor
<?php
class cls
{
function cls1()
{
echo “form cons”;
}
function __construct()
{
echo “form def cons”;
}
function fun()
{
echo “form method”;
}
function __destruct()
{
echo “DES execute”;
}
}
$obj= new cls1();
$obj->fun()
?>

$this:

  • $this is a global variable use to access properties of class from method of that class.
Example for $this
<?php
class cls()
{
var $x=100;
function fun()
{
$x=123;
echo “scott”;
echo $this->x;
}
}
$obj = new cls();
$obj-> fun();
?>

Leave a Reply

Your email address will not be published. Required fields are marked *