OOPS concepts in PHP with realtime examples

OOPS means object-oriented programming. As we all know that object-oriented programming is better than procedural programming. In procedural programming, we can write functions that contain operations on data but in object-oriented programming, we can write functions that contain both data and objects. It is based on classes and objects. Let us discuss the oops concepts in PHP with realtime examples.

Also read, How to display data from database in PHP

Class and object in PHP

  • A class is like the name of an entity such as fruit, vehicles, smartphones, men, lady, father, son baby, etc.
  • It is declared using the class keyword with curly braces. All the codes, functions, and blocks of statements are written inside the class name.
  • A class works as a template for an object and an object is an instance of a class. A class can create multiple objects.
  • When an object is created, it inherits all the properties and methods of its class but the values of the properties should be different.

How to create an object in PHP

An object is created with the help of a new keyword as shown below

$car = new Car();

whereas $car is an object reference variable of the parent class Car. By using this $car object, we can call the values defined in the methods of the parent class Car.

Use of $this keyword in PHP

$this keyword refers to the current object and is available inside the methods defined in the parent class.

Below is an example of $this keyword

<!DOCTYPE html> 
<html>
<body>
<?php
class Car
{
public $name;
function set_name($name)
{
$this->name = $name;
}
}
$car = new Car();
$car->set_name("Toyota");
echo $car->name;
?>
</body>
</html>

Output:-
Toyota
Use of instanceof keyword

With the help of instanceof keyword, we can check whether an object belongs to its parent class or not.

Example

<!DOCTYPE html> 
<html>
<body>
<?php
class Car
{
public $name;
function set_name($name)
{
$this->name = $name;
}
}
$car = new Car();
if($car instanceof Car)
{
echo "Yes, it is an object of Car class";
}
else
{
echo "No, it does not belong to its parent class";
}
?>
</body>
</html>

Output:- Yes, it is an object of Car class

Example of OOPS concept in PHP

This example will display the name and color of two motorcars

<!DOCTYPE html> 
<html>
<body>
<?php
class Car {
// Properties
public $name;
public $color;
// Methods
function set_name($name)
{
$this->name = $name;
}
function get_name()
{
return $this->name;
}
function set_color($color)
{
$this->color = $color;
}
function get_color()
{
return $this->color;
}
}
//create a new object
$first_car = new Car();
$first_car->set_name('Maruti');
$first_car->set_color('Red');
$second_car = new Car();
$second_car->set_name('BMW');
$second_car->set_color('White');
echo "The name of the first car is: ".$first_car->get_name();
echo "<br>";
echo "The color of the first car is: ".$first_car->get_color();
echo "<br>";
echo "The name of the second car is: ".$second_car->get_name();
echo "<br>";
echo "The color of the second car is: ".$second_car->get_color(); ?> </body>
</html>

Output:- The name of the first car is: Maruti
The color of the first car is: Red
The name of the second car is: BMW
The color of the second car is: White

Conclusion:- I hope this tutorial will help you to understand the overview of the OOPS concept. If there is any doubt then please leave a comment below

Originally published at https://pbphpsolutions.com on August 12, 2022.

--

--

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bipsmedium

Bipsmedium

25 Followers

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.