PHP OOP - Classes and Objects

A class is a blueprint for creating objects. It defines properties (variables) and methods (functions).

An object is an instance of a class. It contains the properties and can execute the methods defined in the class.

OOP Case

Let's assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.


When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Define a Class


A class is created using the class keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces:


Syntax:


<?php
  class Fruit {
    // code goes here...
  }
?>

Below we declare a class named Car consisting of two properties ($brand and $color) and method startEngine():

<?php
class Car {
// Properties (variables)
public $brand;
public $color;
// Method (function)
public function startEngine() {
return "Engine started!";
}
}
?>

Note

In a class, variables are called properties and functions are called methods!

Define Objects


Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.


An object is created using the new keyword.


In the example below, $car1and $car2 are instances of the class Car:

<?php
class Car {
public $brand;
public $color;
// Methods
public function set_brand($brand) {
$this->brand = $brand;
}
public function get_brand() {
return $this->brand;
}
}
// Creating objects with constructor
$car1 = new Car();
$car2 = new Car();

$car1->set_brand("Honda");
$car2->set_brand("Ford");

echo $car1->get_brand(); // Output: This is a Honda.
echo "<br>";
echo $car2->get_brand(); // Output: This is a Ford.
?>

Try it yourself

In the example below, we add two more methods to class Car, for setting and getting the $color property:

Example
<?php
class Car {
public $brand;
public $color;
// Methods
public function set_brand($brand) {
$this->brand = $brand;
}
public function get_brand() {
return $this->brand;
}
public function set_color($color) {
$this->color = $color;
}
public function get_color() {
return $this->color;
}
}
// Creating objects with constructor
$car = new Car();
$car->set_color("Blue");
$car->set_brand("Honda");

echo "Name:" . $car->get_brand();
echo "<br>";
echo "Color:" . $car->get_color();
?>

Try it yourself

PHP - The $this Keyword


In PHP, the $this keyword refers to the current instance of the class. It is used inside class methods to access properties and methods of the same object.


Look at the following example:

Example
<?php
class Car {
public $name;
}
$car = new Car();
?>

So, where can we change the value of the $name property? There are two ways:


1. Inside the class (by adding a set_name() method and use $this):

<?php
class Car {
public $name;
function set_name($name) {
$this->name = $name;
}
}
$car = new Car();
$car->set_name("Honda");
echo $car->name;
?>

Try it yourself

2. Outside the class (by directly changing the property value):

<?php
class Car {
public $brand;
}
$car = new Car();
$car->brand = "Honda";
echo $car->brand;
?>

Try it yourself

PHP - instanceof


The instanceof keyword in PHP is used to check if an object belongs to a specific class or inherits from a particular class.

Example
<?php
class Car {}
$myCar = new Car();
if ($myCar instanceof Car) {
echo "Yes, \$myCar is an instance of the Car class.";
} else {
echo "No, \$myCar is not an instance of the Car class.";
}
?>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.