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

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

KUPU178

KUPU178

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

KUPU178

KUPU178

ceri188

ceri188

KUPU178

KUPU178

KUPU178

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

KUPU178

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

KUPU178

daftar ceri188

cery188

ceri188

KUPU178

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

HOKI311

THOR311

RATU311

KUPU178

THOR311

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

KUPU178

THOR311

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

KUPU178

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

THOR311

RATU311

KUPU178

KUPU178

RATU311

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

KUPU178

THOR311

HOKI311

KUPU178

RATU311

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311