Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which can contain data (in the form of fields or properties) and code (in the form of procedures or methods).
While procedural programming focuses on writing functions that act on data, OOP focuses on creating data structures (objects) that bundle both the data and the functions together.
1. The Core Concepts
| Concept | Analogy | Description |
| Class | The Blueprint (e.g., a car design). | A template or definition for creating objects. It defines the properties and methods common to all objects of that type. |
| Object | The Instance (e.g., your specific blue car). | A specific occurrence of a class. When you run a script, the class is used to create an actual object in memory. |
| Property | The Attributes (e.g., color, speed, year). | Variables defined within a class that hold the data (state) of an object. |
| Method | The Actions (e.g., start(), accelerate()). | Functions defined within a class that define the behavior of an object. |
2. Defining a Class
To define a class, you use the class keyword followed by the class name (conventionally capitalized).
Example: Defining a Product Class
<?php
class Product {
// 1. Properties (Attributes/Data)
public $name;
public $price;
// 2. Method (Behavior/Functionality)
public function getPriceWithTax($taxRate) {
$finalPrice = $this->price * (1 + $taxRate);
return round($finalPrice, 2);
}
}
?>
Note: The
publickeyword is an access modifier (covered in the next chapter) that determines where the property or method can be accessed.
3. Creating Objects (Instantiation)
To create an object from a class (a process called instantiation), you use the new keyword.
Example: Creating Product Objects
<?php
// Instantiation: Creating two separate objects from the Product class blueprint
$monitor = new Product();
$keyboard = new Product();
?>
4. Accessing Properties and Calling Methods
You access properties and call methods of an object using the object operator (->).
Example:
<?php
// 1. Assign values to properties for the $monitor object
$monitor->name = "27-inch 4K Display";
$monitor->price = 450.00;
// 2. Call the method for the $monitor object
$taxRate = 0.05; // 5% tax
$finalCost = $monitor->getPriceWithTax($taxRate);
echo "Product: " . $monitor->name . "<br>";
echo "Total Cost (incl. tax): $" . $finalCost . "<br>";
// 3. The $keyboard object has its own separate data
$keyboard->name = "Mechanical Keyboard";
echo "The second object is: " . $keyboard->name;
?>
5. The Constructor Method (__construct())
The constructor is a special method named __construct() that is automatically called when you create a new object (new Product()). It is primarily used to initialize the object’s properties immediately when it is created.
Example: Using a Constructor
<?php
class User {
public $username;
public $email;
// The constructor method
public function __construct($name, $emailAddress) {
$this->username = $name;
$this->email = $emailAddress;
echo "A new User object has been created for " . $this->username . "<br>";
}
// Other methods...
}
// When creating the object, we pass the initial values directly
$admin = new User("JaneDoe", "jane@admin.com");
// The $admin object is immediately usable
echo "User Email: " . $admin->email;
?>
6. The $this Keyword
Inside a class method, you use the special variable $this to refer to the current object.
In the example above, inside the __construct() method, $this->username refers specifically to the username property of the $admin object we are currently building.
Next Steps
This chapter introduced the fundamental concept of Class and Object. To make OOP truly useful, you must learn about controlling access to your properties and the concept of code reusability.
The next advanced chapter will cover “PHP OOP: Inheritance and Access Modifiers.”
