Could we help you? Please click the banners. We are young and desperately need the money
Object-Oriented Programming (OOP) has become a cornerstone of modern software development, and PHP is no exception. Whether you're a beginner or a senior developer refreshing your knowledge, understanding classes and objects in PHP is key to writing clean, maintainable, and scalable code.
In this guide, we’ll break down OOP fundamentals in PHP, walk through hands-on examples, and demonstrate how classes and objects improve your code structure and readability.
A class in PHP is a blueprint for creating objects. It defines properties (variables) and methods (functions) that describe the behavior and state of an object.
class Car {
public $brand;
public $color;
public function startEngine() {
return "The engine has started!";
}
}
In this example, "Car" is a class with two properties and one method.
An object is an instance of a class. Once a class is defined, you can create multiple objects from it, each with its own unique data.
$myCar = new Car();
$myCar->brand = "Toyota";
$myCar->color = "Red";
echo $myCar->startEngine();
// Output:
The engine has started!
Each object can hold different values for its properties while still using the same methods from the class.
Properties define the attributes of an object. They're declared using visibility keywords: "public", "protected", or "private".
Methods define what an object can do. They are functions declared inside a class.
class Person {
public $name;
public function sayHello() {
return "Hello, my name is " . $this->name;
}
}
$person1 = new Person();
$person1->name = "Alice";
echo $person1->sayHello();
// Output:
Hello, my name is Alice
Constructors are special methods automatically called when an object is instantiated.
class Book {
public $title;
public $author;
public function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
public function getSummary() {
return "{$this->title} by {$this->author}";
}
}
$book1 = new Book("1984", "George Orwell");
echo $book1->getSummary();
// Output:
1984 by George Orwell
Inheritance allows one class to inherit properties and methods from another class, promoting code reuse.
class Animal {
public function makeSound() {
return "Some generic animal sound";
}
}
class Dog extends Animal {
public function makeSound() {
return "Woof!";
}
}
$dog = new Dog();
echo $dog->makeSound();
// Output:
Woof!
Encapsulation is the practice of keeping class properties private or protected and controlling access via methods.
class Account {
private $balance = 0;
public function deposit($amount) {
if ($amount > 0) {
$this->balance += $amount;
}
}
public function getBalance() {
return $this->balance;
}
}
$acc = new Account();
$acc->deposit(100);
echo $acc->getBalance();
// Output:
100
Polymorphism allows methods to behave differently based on the object that is calling them.
class Shape {
public function draw() {
return "Drawing a shape";
}
}
class Circle extends Shape {
public function draw() {
return "Drawing a circle";
}
}
class Square extends Shape {
public function draw() {
return "Drawing a square";
}
}
$shapes = [new Circle(), new Square()];
foreach ($shapes as $shape) {
echo $shape->draw() . "<br>";
}
// Output:
Drawing a circle
Drawing a square
If you're an experienced PHP developer, consider the following advanced topics:
Whether you're just starting out or revisiting the fundamentals, understanding classes and objects is crucial to mastering PHP. These OOP principles not only make your code more powerful but also more organized and easier to maintain.
Take time to practice writing your own classes and objects—it's the best way to solidify your understanding.