What is OOP
OOP stands for Object-Oriented Programming. It’s a programming paradigm (a way of structuring your code) that uses “objects” — bundles of data and related functions — to design and organize software.
✅ Key Concepts of OOP in PHP
Here’s a breakdown with simple examples:
1. Class and Object
A class is a blueprint. An object is an instance of that class.
class Dog {
public $name;
public function bark() {
return $this->name . " says Woof!";
}
}
$dog = new Dog();
$dog->name = "Buddy";
echo $dog->bark(); // Output: Buddy says Woof!
2. Encapsulation
Keep data and methods bundled together, and control access using visibility: public
, private
, protected
.
class BankAccount {
private $balance = 0;
public function deposit($amount) {
$this->balance += $amount;
}
public function getBalance() {
return $this->balance;
}
}
3. Inheritance
Child classes can inherit methods and properties from a parent.
class Animal {
public function eat() {
return "Eating...";
}
}
class Cat extends Animal {
public function meow() {
return "Meow!";
}
}
$cat = new Cat();
echo $cat->eat(); // Inherited
echo $cat->meow(); // Cat’s own method
4. Polymorphism
In OOP, it lets different classes respond to the same method name in different ways. Objects can share a common interface but behave differently.
interface Shape {
public function area();
}
class Circle implements Shape {
public function area() {
return "Calculating circle area...";
}
}
class Square implements Shape {
public function area() {
return "Calculating square area...";
}
}
Polymorphism allows loose coupling.
You can use interfaces and abstract classes to enforce polymorphism.
It enables scalability and flexibility in applications.
5. Abstraction
Hides complex logic and exposes only the essential interface. Abstract class
in PHP can contain a __construct
method
abstract class Vehicle {
abstract public function start();
}
class Car extends Vehicle {
public function start() {
return "Car started!";
}
}
<?php
abstract class Animal {
protected string $name;
protected string $species;
// An abstract class can have a constructor
public function __construct(string $name, string $species) {
$this->name = $name;
$this->species = $species;
echo "Animal constructor called for " . $this->name . " (" . $this->species . ")\n";
}
abstract public function makeSound(): string;
public function getName(): string {
return $this->name;
}
public function getSpecies(): string {
return $this->species;
}
}
class Dog extends Animal {
protected string $breed;
public function __construct(string $name, string $species, string $breed) {
// Call the parent constructor to initialize $name and $species
parent::__construct($name, $species);
$this->breed = $breed;
echo "Dog constructor called for " . $this->name . " (" . $this->breed . ")\n";
}
public function makeSound(): string {
return "Woof!";
}
public function getBreed(): string {
return $this->breed;
}
}
class Cat extends Animal {
public function __construct(string $name) {
// Call the parent constructor, providing the species
parent::__construct($name, 'Feline');
echo "Cat constructor called for " . $this->name . "\n";
}
public function makeSound(): string {
return "Meow.";
}
}
// This would result in a fatal error: Cannot instantiate abstract class Animal
// $animal = new Animal("Generic", "Creature");
$dog = new Dog("Buddy", "Canine", "Golden Retriever");
echo $dog->getName() . " says " . $dog->makeSound() . "\n";
echo "Species: " . $dog->getSpecies() . ", Breed: " . $dog->getBreed() . "\n\n";
$cat = new Cat("Whiskers");
echo $cat->getName() . " says " . $cat->makeSound() . "\n";
echo "Species: " . $cat->getSpecies() . "\n";
?>
Why OOP?
- Reusable code (classes can be reused)
- Scalable architecture (especially for large projects)
- Better code organization
- Easier testing and debugging
Comments are closed.