Object oriented programming concepts in php

0
5646

In this session we will study some object oriented programming concepts in the php like classes and constructor.

  • To study the oop’s concept in php follow the steps given below :

 

    1. Install Xampp server in your machine and if it is already installed go to xampp->htdocs folder .

 

    1. Create a new folder say s7 in the htdocs folder.

1

    1. Inside this s7 folder create a new folder called as classes .

2

    1. Inside the classes folder create a file say car.php .

3

    1. Now open the car.php file with notepad++ or any other text editor .

4

    1. Now write the given code in car.php file :

 

<?php
class Car{

    
    private $model;
	private $color;
	private $make;
	
	public function start()
	{
	  echo 'Car is starting....';
	} 
    public function setMake($make)
	{
	  $this->make=$make;
	} 
	
    public function getMake()
	{
	  return $this->make;
	} 
}
?>

Note: Here we will first set the value using the setMake() function and access it using the getMake() function .

    1. Now in the classes folder create another file say index.php .

5

    1. Open it with notepad++.

6

    1. Write the given code in the index.php file

 

<?php include 'classes/car.php';
$car1=new Car();  // Create an object of an car class
$car1->setMake('Honda City');   //Get the element of an car class.
echo $car1->getMake();

?>

 

    1. You will have the following view of your notepad++ window :

v1

    1. Now from the xampp control pannel start the apache and mysql modules and then open up the browser and enter the following url http://localhost/s7/

 

    1. You will have the following output :

v2

    1. We can also use constructors in php , the syntax is :

 

public function __construct()
{
   code..;	
}

 

    1. Now let’s implement constructor in the following example :

 

      1. Create an new folder in the htdocs folder say s8

 

      1. In this s8 folder create another folder called as classes and a new file say index.php

v3

      1. In the classes folder create a new file called as car.php

v4

      1. Now write the given code in the car.php file :

 

<?php
class Car{

    
    private $model;
	private $color;
	private $make;
	
	public function __construct()
	{
	  echo 'Car is starting....';
	} 
  
}
?>

 

      1. Thus your notepad++ window will have the following view :

v5

      1. Now write the given code in index.php file which is present in the s8 folder .

 

<?php include 'classes/car.php';


$car1=new Car();  // Create an object of an car class

?>

 

      1. You will have the following view of your notepad++ window :

v6

      1. Now to get the output just enter the following url http://localhost/s8/

v7

      1. We can also pass parameters to the constructors .

 

      1. Now edit the car.php file and replace the code with the given one :

 

<?php
class Car{

    
    private $model;
	private $color;
	private $make;
	
	public function __construct($model,$make,$color)
	{
	 
	 $this->make=$make;
	 $this->model=$model;
	 $this->color=$color;
	 
	} 
	public function getColor()
	{
	   return $this->color;
	}
  
}
?>

 

      1. You will have the following view of your notepad++ window :

v8

      1. Also edit the index.php file and add the following code in it :

 

<?php include 'classes/car.php';


$car1=new Car('Tata','Accord','Red');  // Create an object of an car class
echo $car1->getColor();               //Call the method getColor() in our car.php file 
?>

 

      1. You will have the following view of your notepad++ window :

v9

      1. Enter the following url in the browser http://localhost/s8/

 

      1. Hence you will have the following output :

v10

Note: If we create an another object say car2 by calling the same function then the previous objects value gets appended to the newly created objects value.

 

 

  • So if you edit the code as shown below :

 
v11
 

  • And run your project the you will have the following output as shown in the image .

 
v12

 

  1. Thus we have implemented OOP’s concepts like Classes and Constructors in PHP .

LEAVE A REPLY

Please enter your comment!
Please enter your name here