Functional or OOP in PHP: What to Choose in 2024?

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Two Worlds in One Language: Why Is This Important?

When we talk about PHP, the classic object-oriented approach (OOP) immediately comes to mind. Classes, inheritance, encapsulation — this is the foundation taught in all courses. But there is another side to the coin — the functional approach. And you know what? PHP can do both, and very well at that.

In this article, we won't argue about which is better. We will figure out when and which approach to use so that your code is not only beautiful but also fast. If you write in PHP and want to go beyond "it just works" — this is for you.



Object-Oriented PHP: A Classic That Never Gets Old

OOP in PHP is about structure and reusability. You create classes that describe the entities of your application: user, order, product. Each class is responsible for its own area of responsibility.



Example: Working with a User via OOP

Let's look at a typical example. Suppose we need to create a user and send them a welcome email.

<?phpclass User {    private string $name;    private string $email;

public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; }

public function sendWelcomeEmail(): void { $subject = "Welcome, {$this->name}!"; $message = "Hello, {$this->name}. We are glad to see you on our website."; mail($this->email, $subject, $message); }

public function getName(): string { return $this->name; }}

$user = new User("Ivan", "ivan@example.com");$user->sendWelcomeEmail();echo "Email sent to user: " . $user->getName();?>

Everything is logical here: data (name, email) and methods (sending an email) are in one place. This is convenient when you have many different operations with one entity.



Pros of OOP in PHP

  • Readability: code is divided into logical blocks, easy to find the desired method.
  • Inheritance: you can create a base class and extend it.
  • Encapsulation: data is protected from accidental changes (private/protected).
  • Polymorphism: one interface — different implementations.


Cons of OOP

  • Overhead: for a simple task (e.g., formatting a string), writing a class is overkill.
  • State: objects store state, which can lead to bugs if mutations are not monitored.
  • Memory: each object takes up more memory than a simple function.


Functional PHP: Minimalism and Purity

Functional programming in PHP is not just "writing a function." It is a philosophy where functions are the main characters. They have no side effects, do not change global state, and always return a predictable result.



Example: The Same Task, but Functional

Let's solve the same task — creating a user and sending an email — but without classes.

<?phpfunction createUser(string $name, string $email): array {    return ['name' => $name, 'email' => $email];}

function sendWelcomeEmail(array $user): void { $subject = "Welcome, {$user['name']}!"; $message = "Hello, {$user['name']}. We are glad to see you on our website."; mail($user['email'], $subject, $message);}

function getUserName(array $user): string { return $user['name'];}

$user = createUser("Ivan", "ivan@example.com");sendWelcomeEmail($user);echo "Email sent to user: " . getUserName($user);?>

Looks simpler? Yes. But there is no encapsulation here — the data is just an array. However, each function does only one action and knows nothing about the "outside world."



Advanced Functional

Blogs

Book Recommendations