If youβre coding PHP without object-oriented programming (OOP), youβre missing out on a powerful way to organize and reuse code!
OOP makes your code cleaner, reusable, and scalableβperfect for real-world applications like e-commerce websites, blog platforms, and APIs.
π― In this guide, youβll learn:
β
What object-oriented programming is
β
How to create PHP classes and objects
β
How to use properties and methods
β
How to build a mini OOP-based project
Letβs dive in! π
1οΈβ£ What Is Object-Oriented Programming (OOP)?
OOP is a way of structuring code using classes and objects.
π‘ Think of a class as a blueprint (e.g., a Car).
π‘ An object is an instance of a class (e.g., a Toyota or a Tesla).
2οΈβ£ Creating a Class in PHP
Example: Defining a Simple Class
<?php
class Car {
// Properties (variables)
public $brand;
public $color;
// Constructor (Runs when an object is created)
public function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
// Method (function inside a class)
public function getDetails() {
return "This is a $this->color $this->brand.";
}
}
// Creating an object (instance of the class)
$myCar = new Car("Toyota", "Red");
// Output car details
echo $myCar->getDetails();
?>
π₯ Whatβs happening?
β
class Car {}
defines the class.
β
Properties store data ($brand
, $color
).
β
A constructor (__construct
) initializes objects.
β
Methods (like getDetails()
) define behavior.
3οΈβ£ Understanding Class Properties and Methods
Example: Using Public and Private Properties
<?php
class User {
public $name;
private $email; // Private property (cannot be accessed outside the class)
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function getEmail() {
return "User email: " . $this->email;
}
}
$user1 = new User("Zero Dev", "zero@zeroexp.dev");
echo $user1->name; // β
Works (public property)
echo $user1->getEmail(); // β
Works (accessing private property via a method)
// echo $user1->email; // β Error (email is private)
?>
π₯ Why use private properties?
β
Encapsulation (hides sensitive data).
β
Restricts direct access to prevent accidental modification.
4οΈβ£ Using Getters and Setters
Instead of accessing properties directly, use getter and setter methods.
Example: Protecting Data with Getters/Setters
<?php
class BankAccount {
private $balance = 0;
public function deposit($amount) {
if ($amount > 0) {
$this->balance += $amount;
return "Deposited: $$amount";
}
return "Invalid amount!";
}
public function getBalance() {
return "Current Balance: $$this->balance";
}
}
$account = new BankAccount();
echo $account->deposit(100);
echo $account->getBalance();
?>
π₯ Why use getters/setters?
β
Protects sensitive data
β
Validates input before modification
5οΈβ£ Inheritance: Reusing Code
Inheritance allows a class to inherit properties and methods from another class.
Example: Parent and Child Classes
<?php
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function makeSound() {
return "$this->name makes a sound.";
}
}
// Dog class inherits from Animal
class Dog extends Animal {
public function makeSound() {
return "$this->name barks.";
}
}
$dog = new Dog("Buddy");
echo $dog->makeSound(); // Output: Buddy barks.
?>
π₯ Why use inheritance?
β
Reduces code duplication
β
Allows better organization
6οΈβ£ Static Methods and Properties
Static properties and methods belong to the class itself, not individual objects.
Example: Using Static Methods
<?php
class MathHelper {
public static function add($a, $b) {
return $a + $b;
}
}
echo MathHelper::add(5, 10); // Output: 15
?>
π₯ When to use static methods?
β
For utility functions (e.g., MathHelper::add()
).
π― Mini Project: OOP-Based User Management
Letβs build a real-world example using OOP.
1οΈβ£ User Class
<?php
class User {
private $name;
private $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function getDetails() {
return "User: $this->name, Email: $this->email";
}
}
?>
2οΈβ£ Admin Class (Extends User)
<?php
class Admin extends User {
public function accessDashboard() {
return "Admin $this->name has access to the dashboard.";
}
}
?>
3οΈβ£ Testing the Classes
<?php
require "User.php";
require "Admin.php";
$user = new User("Zero Dev", "zero@zeroexp.dev");
$admin = new Admin("Admin User", "admin@zeroexp.dev");
echo $user->getDetails();
echo $admin->accessDashboard();
?>
π₯ Whatβs happening?
β
Encapsulated user data
β
Reused code with inheritance
π Final Thoughts
Now you understand the core concepts of OOP in PHP!
β
Classes and Objects
β
Encapsulation (private/protected properties)
β
Inheritance for reusability
β
Static methods for utilities
π Next: Understanding PHP Inheritance and Interfaces
Happy coding! ππ