PHP OOPS - Classs Object

2. PHP Class Object

A Class is a Blueprint for Objects

In Object-Oriented Programming (OOP), a class acts as a blueprint for creating objects. An object is an instance of a class, meaning it is created based on the defined class structure.


OOP Case: Class Example

Let’s imagine we have a class called Vehicle. A Vehicle can have attributes such as brand, model, and price. These attributes can be represented as variables like $brand, $model, and $price.

When individual objects (like Car or Bike) are created from the Vehicle class, they inherit all the properties and behaviors of the class but can hold their own unique values for these properties.


Defining a Class

A class is defined using the class keyword followed by the class name and curly braces {}. Properties and methods go inside the curly braces.

Syntax Example

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

Below, we create a Vehicle class with two properties ($brand and $model) and two methods (set_brand() and get_brand()) for setting and retrieving the $brand property:

 

<?php
class Vehicle {
  // Properties
  public $brand;
  public $model;

  // Methods
  function set_brand($brand) {
    $this->brand = $brand;
  }
  function get_brand() {
    return $this->brand;
  }
}
?>

Creating Objects

A class is just a template; it becomes useful only when we create objects from it. Objects are instances of a class, each having their own set of property values while sharing the same methods.

To create an object, use the new keyword.

Example

<?php
class Vehicle {
  // Properties
  public $brand;
  public $model;

  // Methods
  function set_brand($brand) {
    $this->brand = $brand;
  }
  function get_brand() {
    return $this->brand;
  }
}

$car = new Vehicle();
$bike = new Vehicle();

$car->set_brand('Tesla');
$bike->set_brand('Yamaha');

echo $car->get_brand();
echo "<br>";
echo $bike->get_brand();
?>

Adding More Methods

Let’s expand the Vehicle class by adding methods to set and retrieve the $model property.

Example

<?php
class Vehicle {
  // Properties
  public $brand;
  public $model;

  // Methods
  function set_brand($brand) {
    $this->brand = $brand;
  }
  function get_brand() {
    return $this->brand;
  }
  function set_model($model) {
    $this->model = $model;
  }
  function get_model() {
    return $this->model;
  }
}

$car = new Vehicle();
$car->set_brand('Tesla');
$car->set_model('Model S');

echo "Brand: " . $car->get_brand();
echo "<br>";
echo "Model: " . $car->get_model();
?>

Using $this Keyword

In PHP, the the $this keyword is used within class methods to refer to the current object. This allows you to access or modify the properties of the object.

Example

<?php
class Vehicle {
  public $brand;

  function set_brand($brand) {
    $this->brand = $brand;
  }
}

$car = new Vehicle();
$car->set_brand("Tesla");

echo $car->brand;
?>

Modifying Properties

You can modify the property values of an object either:

  1. Inside the class using a method that employs $this:
    <?php
    class Vehicle {
      public $brand;
    
      function set_brand($brand) {
        $this->brand = $brand;
      }
    }
    
    $car = new Vehicle();
    $car->set_brand("Tesla");
    
    echo $car->brand;
    ?>
    
  2. Outside the class by directly accessing the property:
    <?php
    class Vehicle {
      public $brand;
    }
    
    $car = new Vehicle();
    $car->brand = "Tesla";
    
    echo $car->brand;
    ?>
    

Checking Object Type with instanceof

You can use the instanceof keyword to verify whether an object belongs to a particular class.

Example

<?php
$car = new Vehicle();
var_dump($car instanceof Vehicle); // Output: bool(true)
?>

With this knowledge, you can confidently use classes and objects in PHP to build modular and reusable code!

Exercises:

 

Exercise 1: Creating and Managing a Class

Create a PHP class called “Person” with the following properties and methods:

Properties:

  • $name (public)
  • $age (public)

Methods:

  1. set_name($name) – To set the name of the person.
  2. get_name() – To retrieve the name of the person.
  3. set_age($age) – To set the age of the person.
  4. get_age() – To retrieve the age of the person.

Tasks:

  • Create two objects of the Person class, $person1 and $person2.
  • Set different names and ages for each person using the provided methods.
  • Display the name and age of each person.

Exercise 2: Extending a Class

Build upon the “Vehicle” class provided in the explanation. Add the following:

Additional Properties:

  • $color (public)

Additional Methods:

  1. set_color($color) – To set the color of the vehicle.
  2. get_color() – To retrieve the color of the vehicle.

Tasks:

  • Create an object named $myCar from the Vehicle class.
  • Set the brand, model, and color of the car using the appropriate methods.
  • Print all the details of the car (brand, model, and color) in the following format:
    Brand: Tesla | Model: Model S | Color: Red.

Bonus Challenge:

Add a method to the Vehicle class called display_details() that outputs all the details of the vehicle in one go. Then, use this method to display the details of $myCar.

Leave a Reply

Your email address will not be published. Required fields are marked *