DeveloperJoy Logo DeveloperJoy

PHP Doesn't Suck Anymore

May 3, 2024
PHP Doesn't Suck Anymore

I'm tired of reading the same message all time: "PHP sucks."

But most of these critics haven't looked at PHP since 2012, and a lot has changed since then.

Let's take a look at the language changes that have happened since PHP 5.4 was released.

Traits (PHP 5.4)

PHP 5.4 introduced traits, which allow for composition over inheritance. You can have traits and include them in every class.

trait SayWorld {
    public function sayHello() {
        echo 'World!';

class MyHelloWorld {
    use SayWorld;

Short Array Syntax

Gone are the days of writing array() like a caveman. You can now use square brackets for short array syntax.

$newArray = [

Array Destructuring

Assigning an array to a temporary variable is a thing of the past. You can now use array destructuring to directly assign variables from an array.

$newArray = [

$anotherArray = [

First-Class Variadic Functions

You can pass as many arguments to a function as you want using the ... syntax.

class Variadic {
  public function query($query, ...$attributes) {
    var_dump($query, $attributes);

$v = new Variadic();

$v->query('select * from users', 1, 'Víctor');

// result 'select * from users', 1, 'Víctor'


Need to do something memory-intensive in a memory-efficient way? Generators are the way to go.

Here you can find more info about generators.

Anonymous Classes

Need a new class but can't be bothered to make a new file? Anonymous classes are the solution. They can implement an interface just like any other class.

$anonymousClass = new class{
  public $property = 1;
  public function add(int $x): int
    return $this->property + $x;

echo $anonymousClass->propert; // 1
echo $anonymousClass->add(2);  // 3

Trailing Commas (PHP 7)

No more worrying about adding a trailing comma to a function call or method call.

class TrailingComma {
  public function action(
    int $x,
    int $y, // <- This comma causes an error before
  ) {
    echo $x + $y;

Arrow Functions

PHP has arrow functions too! They're not exactly like JavaScript's, but they're a great addition to the language.

$y = 1;
$fn1 = fn($x) => $x + $y;

$x = $fn1(2); // 3

Null Coalescing Operator (PHP 7)

No more checking for null before assigning a value. The null coalescing operator has got you covered.

// If user is null or undefined, 'nobody' is assigned
$username = $_GET['user'] ?? 'nobody'; 

Null Coalescing Assignment Operator (PHP 7.4)

And if you need to shorthand that null coalescing operator, there's an assignment operator for that too.

// Value of $_GET['user'] change if is undefined or null
$_GET['user'] ??= 'nobody';

Null Chaining Operator (PHP 8)

No more checking for null before calling a method. The null chaining operator is here to save the day.

class Customer {
    public function getAddress(): ?Address {}

class Address {
    public function getCountry(): string {}

// Null if no Address
$country = $customer->getAddress()?->getCountry(); 

Named Arguments (PHP 8)

Sick of using null to skip over optional arguments? Named arguments are the solution.

Imagine that you have a function with a lot of arguments, some or them are nullable.

function takes_many_args(
    $second_arg = 'any',
    $third_arg = 5,
    // ...

Before you have to send the default $second_arg, even if you are not changing it.

takes_many_args('first', 'any', 3);

Now you can just do something like this:

takes_many_args('first', third_arg: 3);

Match Statement (PHP 8)

No more switch statements that are a mile long. The match statement is a more compact and readable way to write switch statements.

$food = 'cake';

$return_value = match ($food) {
  'apple' => 'This food is an apple',
  'bar' => 'This food is a bar',
  'cake' => 'This food is a cake',
  default => 'Unknown food',

var_dump($return_value); // 'This food is a cake'

Enums (PHP 8.1)

Enums are finally here! You can create enum classes with values and methods, and even use them as type hints.

enum Suit
    case Hearts;
    case Diamonds;
    case Clubs;
    case Spades;

function do_stuff(Suit $s)
    // ...


Type Safety

PHP now has typed arguments, return types, union types, intersection types, and more. You can even use type hints for enums!

Now we can add types to arguments, return values, class properties, constants, and almost anything.

Types in PHP

Constructor Property Promotion (PHP 8.0)

Gone are the days of verbose constructors. Constructor property promotion is here to reduce boilerplate code.

This was one of the best additions to PHP, now classes are super simple and we don't need to write classess properties more than once.

class Customer
    public function __construct(
        public string $name, 
        public string $email, 
    ) {}

ReadOnly Properties (PHP 8.1)

And now we don't need to create custom getters and setters and set private visibility to each method.

With the readonly flag we can secure our properties from modifications outside of the class itself.

class Customer
    public function __construct(
        readonly public string $name, 
        readonly public string $email, 
    ) {}

In PHP 8.3 they added anonymous readonly classes.

$class = new readonly class {
    public function __construct(
        public string $name = 'Víctor',
    ) {}


PHP has experienced a 400% performance increase between 5.6 and 7, and another 20% between 7 and 8.

It's fast enough for most use cases, and if you need a specialized use case, use a specialized language.

I have manage more than +725M request per week while working at Wallbox.

In conclusion, PHP is not dead, nor is it sucking anymore. The language has undergone significant changes since 2012, and it's high time to revise our opinions about it.

With the introduction of traits, short array syntax, array destructuring, and a host of other features, PHP has become a more efficient, readable, and maintainable language.

Add to that the improvements in error handling, the introduction of attributes, and the long-awaited arrival of enums, and it's clear that PHP has evolved into a robust and reliable choice for web development.

So, the next time someone tells you that PHP sucks, you can confidently tell them that they're just stuck in the past.


← Go back to the blog

Work with me

Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.

Get 1 month free with yearly plan during checkout.

Technical Advisor

$ 3 ,995


What's included:

  • Lead your team to a better code architecture, testing mentality, clean code, and more.
  • Lead knowledge-sharing sessions depending on current company status and requirements.
  • Help with product and technical decisions.
  • Pause or cancel anytime.

Integrated Senior Developer

$ 5 ,995


Not available

What's included:

  • Includes all Technical Advisor services.
  • Engages as a team member in daily tasks.
  • Participates actively in day-to-day development.
  • Direct communication:
    Slack, and meetings included.
  • Pause or cancel anytime.

Want to talk first?

Ok, just book a call now.