I recently had a few “aha!” moments while revisiting some core Object-Oriented Programming (OOP) concepts.
🛡️ Encapsulation – Protecting the Data
So, this was mind-blowing for me: When we declare a variable as private in a class, it’s not accessible from outside the class — not even if you have the correct object!
I used to think:
echo $obj->name; // This should work, right?
But if name is private, nope — you’ll get an error.
Private means truly private — only the class itself can access it. That’s where getters and setters come in. They let you safely read or update these private properties from outside the class. This whole idea of controlling access to data is called Encapsulation.
Wow, right?
The purpose of getters and setters is to allow controlled access to private or protected properties from outside the class, and for that reason, they must be public.
Why public for getters & setters?
- A getter is used to retrieve the value of a private or protected property from outside the class.
- A setter is used to modify the value of a private or protected property from outside the class.
Since you want to access these methods from outside the class, they must be public.
Why Not Private or Protected for Getters/Setters?
- If a getter or setter were private, you wouldn’t be able to call them from outside the class, even though they exist to allow external interaction.
- Protected would only allow the method to be called from the class or its subclasses, but not from outside of those contexts.
So, to make the class properties accessible safely (while still hiding the actual implementation), public getters and setters are the go-to solution. 😊
☕ Abstraction – Hide the Complexity
Imagine a coffee machine. You just press a button and coffee comes out. You don’t care how it grinds the beans, boils water, or mixes it.
That’s Abstraction — hiding all the complex stuff and showing only what’s necessary to the user. In PHP, we achieve this using abstract classes and interfaces.
👨👩👧 Inheritance – Code Reusability
This one’s the most familiar. Inheritance means one class can extend another and reuse its properties and methods. It helps avoid repeating code.
In PHP:
class Animal {
function speak() {
echo "Animal speaks";
}
}
class Dog extends Animal {
// Inherits speak()
}
Also, if you use the final keyword on a class or method, it cannot be inherited or overridden. That’s it — final means final!
🧩 Polymorphism – Many Forms
Polymorphism allows different classes to respond to the same method in their own way.
Example:
abstract class Animal {
abstract function speak();
}
class Dog extends Animal {
function speak() {
echo "Woof!";
}
}
class Cat extends Animal {
function speak() {
echo "Meow!";
}
}
Here, the same speak() method behaves differently for each animal. That’s polymorphism in action.
🔑 Abstract Classes & Methods – Blueprint for Children
An abstract class cannot be instantiated directly — it’s like a blueprint. You create child classes from it.
If you define an abstract method inside, every child class must implement that method. It’s a great way to make sure certain functionality is always defined in subclasses — sort of like setting rules.
If you’re just getting started or having your own “wow” moments — welcome to the club! 😊
