PHP OOP Classes and Objects
PHP OOP (object-oriented programming) organises code into classes that bundle data and behaviour. Laravel and almost every modern PHP library are built with classes, so understanding OOP is essential.
Defining a Class
A class is a blueprint; an object is an instance created with new. Properties hold data and methods are functions inside the class. $this refers to the current object.
- Constructor property promotion (PHP 8) declares and assigns properties in the constructor signature
<?php
class Product {
public function __construct(
public string $name,
private float $price,
) {}
public function priceWithTax(float $rate = 0.17): float {
return round($this->price * (1 + $rate), 2);
}
}
$p = new Product('Keyboard', 50);
echo $p->name . ": " . $p->priceWithTax();Keyboard: 58.5
Visibility: public, protected, private
- public: accessible from anywhere
- protected: accessible in the class and its child classes
- private: accessible only inside the class itself
- readonly: can be set once (usually in the constructor) and never changed
Inheritance
A child class extends a parent, inherits its members and can override methods. parent:: calls the parent version.
<?php
class Animal {
public function __construct(protected string $name) {}
public function speak(): string {
return "{$this->name} makes a sound";
}
}
class Dog extends Animal {
public function speak(): string {
return "{$this->name} barks";
}
}
foreach ([new Animal('Cat'), new Dog('Rex')] as $a) {
echo $a->speak(), "\n";
}Cat makes a sound Rex barks
Interfaces and Abstract Classes
An interface is a contract listing methods a class must implement. An abstract class can contain shared code but cannot be instantiated. A class can implement many interfaces but extend only one class.
<?php
interface Shape {
public function area(): float;
}
final class Circle implements Shape {
public function __construct(private readonly float $r) {}
public function area(): float { return round(M_PI * $this->r ** 2, 2); }
}
final class Square implements Shape {
public function __construct(private readonly float $side) {}
public function area(): float { return $this->side ** 2; }
}
$shapes = [new Circle(2), new Square(3)];
echo array_sum(array_map(fn(Shape $s) => $s->area(), $shapes));21.57
Static Members and Namespaces
Static properties and methods belong to the class, not an object, and are called with ::. Namespaces group classes and avoid name clashes; Composer autoloading loads them automatically.
<?php
namespace App\Support;
class Str {
public static function slug(string $text): string {
return strtolower(trim(preg_replace('/[^A-Za-z0-9]+/', '-', $text), '-'));
}
}
echo Str::slug('Learn PHP OOP Today!');learn-php-oop-today
Frequently asked questions
What is constructor property promotion in PHP?
A PHP 8 shortcut where you add visibility to constructor parameters, for example public function __construct(public string $name) {}, so PHP declares and assigns the property for you.
What is the difference between an interface and an abstract class in PHP?
An interface only defines method signatures and a class can implement many. An abstract class can include shared code and properties, but a class can extend only one.
When should I use static methods?
For helpers that do not need object state, such as factory methods or pure utility functions. Overusing static makes code harder to test.