OOPS concepts in PHP with realtime examples


OOPS concepts in PHP with realtime examples

OOPS means object-oriented programming. As we all know 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 real-time 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


Leave a Comment

THOR311

RATU311

RATU311

THOR311

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

RATU311

RATU311

THOR311

parlay

HOKI311

RATU311

RATU311

judi bola online

Mix Parlay

THOR311

Ceri188

Thor311 Game Online

Thor311 Aplikasi

LINK CERI188

RATU311 NONTON BOLA

RATU311

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

THOR311 LOGIN

THOR311

THOR311

RATU311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

RATU311

THOR311

THOR311

HOKI311 Link Apk Resmi

RATU311

KUPU178

RATU311

RATU311

RATU311

CERI188

CERI188 Situs Spaceman Terbaik 2026

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188 Live Casino

HOKI311 Lapak Arena Slot

RATU311 judi bola

CERI188 Live Casino

HOKI311 Link Spaceman Online

RATU311

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

KUPU178

KUPU178

KUPU178

HOKI311 TEMBAK IKAN ONLINE

RATU311

CERI188 PANDUAN MAXWIN SLOT ONLINE

RATU311

RATU311

RATU311

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

link login CERI188

link login CERI188

link daftar CERI188

THOR311

HOKI311

HOKI311

CERI188

CERI188

CERI188

parlay

THOR311

RATU311

RATU311

HOKI311

HOKI311

parlay

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

RATU311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

situs bola

THOR311

slot gacor

slot gacor

yakuza303

slot gacor

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

mix parlay

judi bola

judi bola

mix parlay

parlay

judi bola

sv388

judi bola

judi bola

judi bola

parlay

THOR311