Object-oriented PHP – classes, objects, and the main concepts that let you organise code into reusable pieces.
What is OOP?
Object-oriented programming groups data and behaviour together in ‘objects’. An object holds properties (data) and methods (functions). A class is the template; an object is an instance of that class.
Why use OOP in PHP?
It helps manage complexity in larger applications, keeps code DRY (Don’t Repeat Yourself), and makes things easier to maintain and extend.
Classes
A class defines the properties and methods that its objects will have. Think of it as a blueprint.
Defining a class
Use the class keyword.
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
?>
Creating objects
An object is an instance of a class. Create one with new.
<?php
$myCar = new Car("black", "Volvo");
echo $myCar->message();
?>
Properties and methods
These are the two main parts of a class.
Properties
Variables inside a class.
<?php
class Car {
public $color;
public $model;
// ...
}
?>
Methods
Functions inside a class.
<?php
class Car {
// ...
Public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
?>
The __construct() method
Called automatically when an object is created. Commonly used to set initial property values.
<?php
class Car {
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
// ...
}
?>
Visibility: public, private, and protected
Visibility keywords control who can access properties and methods.
- Public: accessible from anywhere.
- Private: accessible only within the class.
- Protected: accessible within the class and its subclasses.
<?php
class Car {
private $model;
public function setModel($model) {
$this->model = $model;
}
public function getModel() {
return $this->model;
}
}
?>
Inheritance
A child class extends a parent class and inherits its properties and methods.
<?php
class Vehicle {
public $color;
public function message() {
return "My vehicle is " . $this->color . "!";
}
}
class Car extends Vehicle {
public $model;
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
?>
Interfaces and abstract classes
Both define methods that child classes must implement.
- Interfaces: declare methods without implementing them.
- Abstract classes: can include both declared and implemented methods.
<?php
interface VehicleInterface {
public function changeGear($gear);
}
class Car implements VehicleInterface {
public function changeGear($gear) {
echo "Changing gear to: $gear";
}
}
?>
Polymorphism
Different classes can respond to the same method call in different ways.
<?php
class Car {
public function intro() {
echo "Choose your dream car!";
}
}
class Volvo extends Car {
public function intro() {
echo "Proud to be Swedish!";
}
}
?>
Best practices
- Encapsulation: keep each class focused on one job.
- Reuse code: use inheritance and interfaces rather than copying logic.
- Plan ahead: think about how your classes will interact before you start building.
Start with classes, objects, and properties. Add inheritance and interfaces once those feel natural.

