A New Way of Thinking
So far, we have been writing code in a "procedural" way—a list of instructions for the server to follow from top to bottom. Object-Oriented Programming (OOP) is a different paradigm. Instead of a list of steps, we think about our program in terms of "objects" that model real-world things.
An object bundles together its own data (called properties) and the functions that operate on that data (called methods). This keeps your code organized, reusable, and easier to manage as projects grow larger.
The Blueprint Analogy
The easiest way to understand OOP is with an analogy:
- A Class is like a blueprint for a house. It defines all the general characteristics (properties) a house can have, like number of rooms or color, and all the things a house can do (methods), like opening a door or turning on the lights.
- An Object is the actual house you build from that blueprint. You can build many houses (objects) from the same blueprint (class), but each one is a separate instance. One house might be blue, while another is red.
1. Classes and Objects
A Class is the template, and an Object is an instance of that class. You create a class using the class
keyword and create an object from it using the new
keyword.
<?php
// This is the blueprint (Class)
class Car {
// Properties and methods will go here
}
// Now, let's build a car (Object) from the blueprint
$myCar = new Car();
var_dump($myCar);
?>
2. Properties (The Data)
Properties are variables that belong to a class. They define the characteristics of an object.
<?php
class Car {
// Properties
public $color;
public $model;
}
$car1 = new Car();
// Use the object operator `->` to set a property's value
$car1->color = "Red";
$car1->model = "Toyota";
echo $car1->model; // Outputs: Toyota
?>
3. Methods (The Actions)
Methods are functions that belong to a class. They define what an object can do.
<?php
class Dog {
public $name;
// This is a method
public function bark() {
return "Woof! Woof!";
}
}
$myDog = new Dog();
$myDog->name = "Buddy";
// Call the method on the object
echo $myDog->name . " says: " . $myDog->bark();
?>
Putting It All Together
Let's combine these concepts into a full example. Notice the special keyword $this
, which is used inside a class to refer to the current object.
<?php
class User {
// Properties
public $username;
public $email;
// A method to display user information
public function displayInfo() {
return "Username: " . $this->username . ", Email: " . $this->email;
}
}
// Create the first user object
$user1 = new User();
$user1->username = "Alex";
$user1->email = "alex@example.com";
// Create a second, independent user object
$user2 = new User();
$user2->username = "Maria";
$user2->email = "maria@example.com";
// Call the method on each object
echo $user1->displayInfo(); // Outputs: Username: Alex, Email: alex@example.com
echo "<br>";
echo $user2->displayInfo(); // Outputs: Username: Maria, Email: maria@example.com
?>
This is just the beginning of OOP. It's a huge topic, but understanding classes, objects, properties, and methods is the first and most important step. In Part 3, we'll explore advanced OOP concepts and see how modern PHP frameworks use these ideas to build powerful applications.