Advertisement
Beginner PHP, OOP

What are the four core principles of object oriented programming, and how does PHP support each of them?

Short answer

The four core principles are encapsulation, which means bundling data and the methods that operate on it together while controlling access using visibility modifiers, inheritance, which lets a class inherit properties and methods from a parent class, polymorphism, which lets…

Save PHP mock interview
Advertisement
AModel answer
The four core principles are encapsulation, which means bundling data and the methods that operate on it together while controlling access using visibility modifiers, inheritance, which lets a class inherit properties and methods from a parent class, polymorphism, which lets different classes be used interchangeably through a shared interface or parent class, and abstraction, which means exposing only essential details while hiding complex implementation, and PHP fully supports all four of these principles through its class, interface, and visibility keyword syntax.
class Animal {
    protected $name;
    public function makeSound() { return 'Some sound'; }
}
class Dog extends Animal {
    public function makeSound() { return 'Woof'; }
}
Real-world example
A pet management application defines a base Animal class with shared properties and behavior, then creates specific Dog and Cat subclasses that each override the makeSound method appropriately, demonstrating inheritance and polymorphism working together.
Likely follow-ups
  • How do these four principles work together in a typical well designed class hierarchy?
  • Which of these four principles is generally considered the most fundamental?
Related topics