Basic OOP Concepts

You can develop/write object-oriented applications in PHP. Object oriented programming helps to understand and implement read world entities easily.

● First understand the following concepts to do object oriented programming.!

  • Class
  • Object
  • Member Variable
  • Member Function
  • Inheritance
  • Parent Class
  • Child Class
  • Overriding
  • Constructor
  • Destructors

● Classes

A Class is a blueprint for an object. And an object encapsulates conceptually related State and Responsibility of something in your Application and usually offers an programming interface with which to interact with these.

● Objects

An Object is a specific instance of a class. For example, if you create a studentclass, you might the go on to create an object stu that belongs to the student class. You could then create a second object,yourstu, also based on the studentclass.

● Properties

In OOP terminology, the characteristics of a class or object or known as itsproperties.

● Methods

The behaviours of a class- that is, the actions associated with the class- are known as its methods. Methods are very similar to functions; in fact, you define methods in PHP using the function statement.

Creating Classes and Objects in PHP

To create a class, you use PHP's class keyword. Here's a really simple class:

<?php
class student //create class
{
    var $roll =12;
    var $name ="Raj";
}
$stu =new student; //create object of student class
 echo $stu->roll;
 echo "<br/>".$stu->name;
?>

Output is:
12
Raj

Example:

<?php
class emp
{
//Member variable
var $ecode; 
var $ename;
//Member Function
public function setdata($ecode, $ename)
{
    $this->ecode=$ecode;
    $this->ename=$ename;
}
public function getdata()
{
	echo $this->ecode;
	echo "<br/>".$this->ename;
}
}
$e= new emp; // Create Object
$e->setdata(123,"Aman"); // calling Member Function
$e->getdata(); // calling Member Function
?>

Output is:
123
Aman

Constructors

PHP5 allows developer to declare constructor methods for classes. Classes which have a constructor methods call this methods on each newlly created object, so it is sutaible for any initialization that the object may need before it is used.

Example:

<?php
class employee
{
    public $code;
    public $ename;

public function __construct()
{
	 $this->code=103;
	 $this->ename="Anurag";
}
}
$em=new employee();
echo $em->code;
echo "<br/>".$em->ename;
?>

Output is:
103
Anurag
Through construct with Parameters:

<?php
class emp_abc
{
    var $code;
    var $name;
	function __construct($c, $n)// passing parametrs
	{
		$this->code=$c;
		$this->name=$n;
	}
}
$e= new emp_abc(102,"wilium"); 
echo $e->code;
echo "<br/>".$e->name;
?>

Output is:
102
wilium
Making our class member as private:

<?php
class emp_ab
{
    private $code;
    private $name;
	function __construct($c, $n)
	{
		$this->name=$n;
		$this->code=$c;
	}
	function shiw_data()
	{
		echo $this->name;
		echo "<br/>".$this->code;
	}
}
$s=new emp_ab("Mack", 122);
$s->shiw_data();
?>

Output is:
Mack
122

Destructor

The destructor method will be called as soon as there are no other referances to a particular object, or in order during the shutdown sequence.

Syntax:-      [void__destruct()]
Examples

<?php
class emp_des
{
    private $message;
     function __construct()
	 {
		 $this->message="SSSOFTAID";
		 echo $this->message;
	 }
	 function _mymess()
	 {
		 $this->message="KARAN";
		 echo "<br/>".$this->message;
	 }
	 function __destruct()
	 {
		 $this->message="It is default in  last to print";
		 echo "<br/>".$this->message;
	 }
}
$d= new emp_des();
$d->_mymess();
?>

Output is:
SSSOFTAID
KARAN

self:: keyword

Now, $this is a good way of referancing properties, but how do we referance Method?.

Example

<?php
class softaid
{
    private $msg;
    function __construct($messages)
	{
		$this->msg=$messages;
		self::_shows();//calling the methods by self::
	}
	function _shows()
	{
		echo "<br/>".$this->msg;
	}
}
$g=new softaid("PHP Developers");
$g->_shows(); 
?>

Output is:
PHP Developers
PHP Developers

PHP Include File

The include(or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

The basic difference between include & require

The include and require statements are identical, except upon failure:

  • require will produce a fatal error and stop the script.
  • include will only produce a warning and the script will continue.

Example

<?php
include('emps.class.php');

class empa extends employees
{
    function _showe($ec,$en,$ej)
    {
		$this->ecode=$ec;
		$this->ename=$en;
		$this->job=$ej;
	}
	function _show_code()
	{
		echo "<br/>". $this->ecode;
		self::_show_sr();// call the method from  other class
	}
}
$sw= new empa;
$sw->_showe(1234, "karan maurya", "Manager");
echo $sw->ename;
echo "<br/>". $sw->job;
echo $sw->_show_code();
echo "<br/>".$dev;

?>

It's Class page(emps.class.php) include in (objects.php)page:

<?php
$dev="Hi, How are you?";
class employees
{
 private $srn;
 protected $ecede;
 public Sename;
 public $job;
 function _shows_sr ()
 {
 $this—>srn=1O8;
 echo "<br/>" . $this—>srn;
 }
}
?>

Output is:
karan maurya
Manager
1234
108
Hi, How are you?

● Inheritance

Inheritance is used in Object Oriented PHP programming to reduce code duplication. It allows a class to inherit members from another class by using the extend clause.

Syntax:

class child extends parant
{
defimition body...
}

● Example


<?php
   class  employee
   {
         var $ename;
	  var $dept;
	  var $basic;
   }
   
   
   class   classA  extends employee
   {
	       function __construct()
	 	   {
		     echo "<br/>----------------------";		   
			 echo "<br/> Class A Employee";
			 echo "<br/>----------------------";		   
		   }
          function   pay($nma,$dept,$basic)
		  {
			  $this->ename=$nma;
  			  $this->dept=$dept;
			  $this->basic=$basic;
			  
			  $hra=$this->basic*.10;
			  $da=$this->basic*.40;
			  $cca=$this->basic*.05;
			  $pay= $this->basic+ $hra +$cca+$da;
			  echo "<br/> Name:".$this->ename;
			  echo "<br/> Dept:".$this->dept;
 			  echo "<br/> Net Pay:".$pay;
						  
			  
			  
		  }
   }
   
   class   classB  extends employee
   {
   	       function __construct()
	 	   {
		   echo "<br/>----------------------";		   
		   echo "<br/> Class B Employee";
		   echo "<br/>----------------------";		   
		   }

          function   pay($nma,$dept,$basic)
		  {			  
		      $this->ename=$nma;
  			  $this->dept=$dept;
			  $this->basic=$basic;
			  $da=$this->basic*.40;
			  $pay= $this->basic+ $da;
			  echo "<br/> Name:".$this->ename;
			  echo "<br/> Dept:".$this->dept;
 			  echo "<br/> net Pay:".$pay;
		  }
   }
      
   $a=new classB;
   $a->pay("Prag","Sales",1000);
      
   $a=new classA;
   $a->pay("Prag","Sales",1000);
?>

output

----------------------
Class B Employee
----------------------
Name:Prag
Dept:Sales
net Pay:1400
----------------------
Class A Employee
----------------------
Name:Prag
Dept:Sales
Net Pay:1550

● Function Overriding

Function definitions in child class override definitions whith same name in parent classes. In a child class, we can modify the difinition of a function inherited from parent class.

Example

<?php
class A
{
    function show()
    {
		echo "Here is base class's";
	}
	}
	class B extends A
	{
		function show()
		{
		
		parent::show();
		echo "<br/>","Here is derive class";
		}
	}
	
	$b=new B;
	$b->show();

?>

Output is
Here is base class's
Here is derive class

Static Keyword

Declaring class properties or methods as static makes them accessible without needing instatiation of the class property declared as static cannot be accessed whih an instantiated class object (through a static method can).

Static Variable

The type of variable scoping that i discussed is known as static . In contrast to the variable declared as function parameters, which are distroyed on the functions's exit, a static varaible will not lose its value when the function exits and will still hold that value should the be called again.

Example

<?php
function increament()
{
    $c=1;
    echo "<br/>",$c;
	$c=$c+1;
}
function in()
{
	static $c=1;
	echo "<br/>",$c;
	$c=$c+1;
}
increament();
increament();
increament();
in();
in();
in();
?>

Output is:
Method call
1
1
1
Static method call
1
2
3
Example

<?php
class abc
{
    public static $co="SOFTAID";
    public $d= "STUDENTS";
}
$b= new abc;
echo "<br/>".abc::$co;

?>

Output is:
SOFTAID
If Static method in class:

<?php
class abcd
{
    protected static $roll=1;
    protected $cnt=0;
	function __construct()
	{
		$this->cnt=$this->cnt+1;
	}
	static function display()
	{
		self::$roll=self::$roll+1;
		echo "<br/>static".self::$roll;
	}
	function _display()
	{
		echo"<br/>".$this->cnt;
	}
}
$a= new abcd();
$a->_display();

abcd::display();

$ab= new abcd();
$ab->_display();

$ac=new abcd();
$ac->_display();

Output Is:

1
static2
1
1
Example
Extesnd class through static:

<?php
class foo
{
    public static $mg_static='softaid';
    public function staticvalue()
	{
		return self::$mg_static;
	}
}
class bar extends foo
{
	public function foostatic()
	{
		return self::$mg_static;
	}
}
print "<br/>". foo::$mg_static."\n";
$foo=new foo();
print $foo->staticvalue()."\n";
print "<br/>".Foo::$mg_static."\n";
print "<br/>".Bar::$mg_static."\n";
$f=new bar();
print $f->foostatic()."\n";
?>

Output Is:

softaid softaid
softaid
softaid softaid