Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design software. PHP supports OOP, allowing developers to create modular, reusable code. OOP helps in organizing complex programs and makes it easier to manage and maintain code.
class Car { public $brand; // Public property public $model; // Public property public function __construct($brand, $model) { $this->brand = $brand; $this->model = $model; } public function honk() { return "Beep!"; } } // Creating an object of the Car class $myCar = new Car("Toyota", "Corolla"); echo $myCar->honk(); // Output: Beep! ?>
Access modifiers control the visibility of properties and methods in a class. PHP has three main access modifiers:
class BankAccount { private $balance; // Private property public function __construct($balance = 0) { $this->balance = $balance; } public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; // Accessing private property } } public function getBalance() { return $this->balance; // Accessing private property } } $account = new BankAccount(); $account->deposit(100); echo $account->getBalance(); // Output: 100 ?>
class Math { public static $pi = 3.14; // Static property public static function areaOfCircle($radius) { return self::$pi * $radius * $radius; // Using static property } } echo Math::areaOfCircle(5); // Output: 78.5 ?>
class User { public $name; public function __construct($name) { $this->name = $name; // Initialize property echo "User {$this->name} created.<br>"; } public function __destruct() { echo "User {$this->name} destroyed.<br>"; // Cleanup } } $user1 = new User("Alice"); // Output: User Alice created. unset($user1); // Output: User Alice destroyed. ?>
Inheritance allows one class (child class) to inherit properties and methods from another class (parent class). This promotes code reusability and establishes a relationship between classes.
class Animal { public function speak() { return "Animal speaks"; } } class Cat extends Animal { // Cat inherits from Animal public function speak() { return "Meow"; } } class Dog extends Animal { // Dog also inherits from Animal public function speak() { return "Woof"; } } $myCat = new Cat(); $myDog = new Dog(); echo $myCat->speak(); // Output: Meow echo $myDog->speak(); // Output: Woof ?>
Polymorphism allows different classes to use the same method name but behave differently. This is useful when you want to treat different objects in a similar way.
class Bird { public function fly() { return "Flies in the sky"; } } class Penguin extends Bird { public function fly() { return "Cannot fly"; } } function makeBirdFly($bird) { echo $bird->fly(); } $sparrow = new Bird(); $penguin = new Penguin(); makeBirdFly($sparrow); // Output: Flies in the sky makeBirdFly($penguin); // Output: Cannot fly ?>
Object-Oriented Programming (OOP) in PHP is a powerful way to organize your code using classes and objects. By understanding the basic concepts of classes, access modifiers, static properties and methods, constructors, destructors, inheritance, and polymorphism, you can write cleaner and more efficient code. OOP makes it easier to manage and reuse your code, which is essential for building larger applications.
By following best practices and being aware of common pitfalls, you can leverage the full potential of OOP in your PHP projects. Start using OOP in your applications today to improve maintainability, scalability, and overall code quality.
We are Recommending you:
- Top VS Code Extensions for 2025 – Super Easy Picks!
- Convert a .pem file into a .ppk
- Best Practices for Microservices
- SQL vs. MongoDB
- ChatGPT vs. DeepSeek: A Simple Comparison
- 5 Lesser-Known WordPress Plugins to Supercharge Your Website in 2025
- Indian currency INR symbol on pdf using dompdf
- Git Commands: A Complete Guide for Developers
- The Developer’s Guide to Debugging WordPress Like a Pro
Master Your Time with the 80/20 Rule: A...
Get Control of Your Time: 6 Easy Ways...
India’s startup space is booming in 2025....
India breeds dreamers who build empires....
VS Code is awesome for coding, and...
Step Out of Your Comfort Zone: 10 Powerful...
1. What is database?A database is a...
Zip is a command-line utility tool used for...
In today’s world, mental agility is a...