Engineering
Understanding OOPs Concepts in PHP — A Beginner-Friendly Guide
Advantage AI Engineering · · 14 min read

A clear walkthrough of classes, objects, visibility, inheritance, polymorphism, encapsulation, abstraction, and interfaces vs abstract classes in PHP—with small code examples you can run and extend.
Object-oriented programming (OOP) in PHP helps you write structured, reusable, and scalable code. Whether you are scripting a small feature or a full web application, OOP aligns with how modern frameworks (Laravel, Symfony, and others) are built. In this guide we break down core ideas—classes, objects, inheritance, encapsulation, polymorphism, and abstraction—using short examples and practical language.
Class vs object
Think of a class as a blueprint and an object as the real thing built from that blueprint. The blueprint (class) says what properties exist and what methods are allowed; each house you build (object) is a specific instance. A class is generic; an object is specific. The class defines members; the object is an instance you create with new. You instantiate objects—you do not “instantiate” a class in the same sense; the class is the template.
A simple PHP class
A class groups properties (data) and methods (behavior). In the example below, $postCode is a property and ringBell() is a method. Each has a visibility keyword (here, public).
<?php
class House
{
public string $postCode = "";
public function ringBell(): void
{
echo "Ding at {$this->postCode}
";
}
}
$home = new House();
$home->postCode = "201301";
$home->ringBell();The new keyword creates an object from the class. You can create many objects from the same class, each with its own property values.
Visibility: public, protected, private
Every property and method has a visibility level that controls who can read or call it from outside the class hierarchy.
- public — callable and readable from anywhere; in modern PHP, members have no default visibility in some contexts, so be explicit for clarity.
- protected — only the class itself and its child classes can access the member.
- private — only the defining class can access the member, not even subclasses.
Encapsulation (hiding internal state) is implemented largely through private or protected data plus public methods that safely expose behavior.
Inheritance
Inheritance lets a child class reuse and extend a parent. The child inherits public and protected members. You declare it with the extends keyword.
<?php
class Shape
{
public function label(): string
{
return "shape";
}
}
class Circle extends Shape
{
public function label(): string
{
return "circle";
}
}
$c = new Circle();
echo $c->label(); // circleWhat is inherited and how it can be overridden depends on visibility and whether methods are redeclared in the child.
Polymorphism
Polymorphism means “many shapes”: one interface or parent type can refer to different concrete classes, and behavior varies by the actual object at runtime. In PHP, type hints make this explicit.
<?php
class Shape
{
public function label(): string
{
return "shape";
}
}
class Circle extends Shape
{
public function label(): string
{
return "circle";
}
}
class Triangle extends Shape {}
function describe(Shape $shape): void
{
echo $shape->label() . PHP_EOL;
}
describe(new Circle()); // circle
describe(new Triangle()); // shape (inherited default)The function describe() only knows about Shape, but Circle and Triangle can be passed in because they extend Shape—assuming label() exists on the hierarchy you design. That interchangeability is polymorphism in practice.
Encapsulation
Encapsulation hides internal state so callers use methods instead of touching raw fields. You can change storage (memory, file, database) later without breaking callers as long as the public API stays stable.
<?php
class Person
{
private string $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
}
$p = new Person("Ada");
echo $p->getName();Users of Person rely on getName(), not on how $name is stored—classic encapsulation, enforced by private.
Abstraction
Abstraction means modeling essentials of a real-world idea without exposing every implementation detail. You focus on what an object does for callers rather than every wire behind the panel.
<?php
class Tv
{
private bool $on = false;
public function powerOn(): void
{
$this->on = true;
}
public function powerOff(): void
{
$this->on = false;
}
}This tiny Tv class abstracts “power state” only—you could extend it later with channels or inputs without changing how users think about turning it on or off.
Interface vs abstract class
Interface
An interface declares methods that implementing classes must define. You cannot instantiate an interface—it is a contract only.
<?php
interface Vehicle
{
public function startEngine(): void;
}
class Car implements Vehicle
{
public function startEngine(): void
{
echo "Car starts
";
}
}
$car = new Car();
$car->startEngine();Abstract class
An abstract class can contain both concrete methods and abstract methods that subclasses must implement. Unlike an interface, it can hold properties and normal method bodies. Important: you cannot instantiate an abstract class directly—you must instantiate a concrete subclass.
<?php
abstract class VehicleBase
{
abstract public function startEngine(): void;
public function stopEngine(): void
{
echo "Engine off
";
}
}
class Truck extends VehicleBase
{
public function startEngine(): void
{
echo "Truck starts
";
}
}
$t = new Truck();
$t->startEngine();
$t->stopEngine();Choose interfaces when you want pure contracts across unrelated classes; choose abstract classes when you want to share real implementation code with subclasses while forcing certain methods to be implemented.
Next steps
Experiment in a single PHP file or REPL, then apply these patterns to namespaced code in a small project. Modern PHP also adds enums, readonly properties, and constructor promotion—natural extensions once the basics above feel comfortable.