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 :
- Install Xampp server in your machine and if it is already installed go to xampp->htdocs folder .
- Create a new folder say s7 in the htdocs folder.
- Inside this s7 folder create a new folder called as classes .
- Inside the classes folder create a file say car.php .
- Now open the car.php file with notepad++ or any other text editor .
- 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 .
- Now in the classes folder create another file say index.php .
- Open it with notepad++.
- 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(); ?>
- You will have the following view of your notepad++ window :
- 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/
- You will have the following output :
- We can also use constructors in php , the syntax is :
public function __construct() { code..; }
- Now let’s implement constructor in the following example :
- Create an new folder in the htdocs folder say s8
- In this s8 folder create another folder called as classes and a new file say index.php
- In the classes folder create a new file called as car.php
- 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....'; } } ?>
- Thus your notepad++ window will have the following view :
- 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 ?>
- You will have the following view of your notepad++ window :
- Now to get the output just enter the following url http://localhost/s8/
- We can also pass parameters to the constructors .
- 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; } } ?>
- You will have the following view of your notepad++ window :
- 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 ?>
- You will have the following view of your notepad++ window :
- Enter the following url in the browser http://localhost/s8/
- Hence you will have the following output :
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 :
- And run your project the you will have the following output as shown in the image .
- Thus we have implemented OOP’s concepts like Classes and Constructors in PHP .