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:
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
Adding More Methods
Let’s expand the Vehicle class by adding methods to set and retrieve the $model property.
Example
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
Modifying Properties
You can modify the property values of an object either:
- Inside the class using a method that employs
$this: - Outside the class by directly accessing the property:
Checking Object Type with instanceof
You can use the instanceof keyword to verify whether an object belongs to a particular class.
Example
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:
set_name($name)– To set the name of the person.get_name()– To retrieve the name of the person.set_age($age)– To set the age of the person.get_age()– To retrieve the age of the person.
Tasks:
- Create two objects of the
Personclass, $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:
set_color($color)– To set the color of the vehicle.get_color()– To retrieve the color of the vehicle.
Tasks:
- Create an object named $myCar from the
Vehicleclass. - 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.
