DEV Community

Cover image for PHP 8.4: Top Features and Improvements
Olushola O.
Olushola O.

Posted on • Originally published at iamgeekygeeky.Medium

PHP 8.4: Top Features and Improvements

PHP 8.4 is finally here, bringing exciting changes that are set to transform the way developers work! With every new version, PHP keeps proving why it’s such an essential tool in today’s web development landscape.

Prerequisites

This article assumes you have basic knowledge of the PHP programming language.

Software I Use for PHP Development

  • Laravel Herd: used to manage my PHP versions and Nginx server.

  • PHPStorm: A great IDE with good IntelliSense and AI copilot.

  • Laragon: An easy-to-use local development environment that supports PHP and other technologies.


Asymmetric Property Visibility (version 2)🔓🔒

In PHP, the visibility of object properties has traditionally been symmetric. This means that the ⁣get and set operations for a property must share the same visibility—public, private, or protected—but cannot differ.

For instance, if a property is public, both reading and writing to it are public, with no way to allow one operation without the other.

In context, when you declare a property of a class public, it becomes mutable, allowing it to be read and modified from outside the class.

However, with the advent of asymmetric visibility, you can now define separate scopes for reading and writing properties.

This means a property can be readable in one context and writable in another, offering greater control over how properties are accessed and modified.

class Animal{
  public private(set) string $name;

  public setName(string $foo){
    $ths->name = $foo;
  }
}

$animal = new Animal();

echo $animal->name; // This will run correctly
Enter fullscreen mode Exit fullscreen mode

We can have a property made public and the set property is made private. This means the property cannot be updated outside the class, making it immutable.

If you try to modify the $name property, you will get an error showing that you cannot modify the property because of the visibility scope.

Error trying to set private property (author’s image)

Here are a few key points to note about asymmetric visibility:

  • Spaces are not allowed in the set-visibility declaration. private(set) is correct. private( set ) is not correct and will result in a parse error.

  • If a property is declared as public, the main visibility can be omitted. For instance, public private(set) and private(set) will behave identically, as the public visibility is implied.

  • Only typed properties are allowed to have separate visibility for set operations. This means you cannot apply asymmetric visibility to untyped properties in PHP.

  • The set visibility must be the same as or more restrictive than the get visibility. For example, public protected(set) and protected protected(set) are valid, but protected public(set) will result in a syntax error.

Find out more about asymmetry visibility, including other examples for your perusal.

Property hooks 🪝

Property hook is a great feature of PHP 8.4 that introduces a way for developers to add get and set directives directly to a variable without expressively creating methods to read and write to the variable.

Alternatively,__get and __set magic methods can be used, but this makes the code more verbose, can introduce errors, and breaks static analysis tools.

It is safe to say the design and syntax of property hooks is similar to that of Kotlin but is mostly influenced by C# and Swift programming languages.

In PHP 8.3, we can create a class with a property in its constructor, and it gives us the capability to read and write to the property.

class Car {
    public function __construct(public string $model) { }
}
Enter fullscreen mode Exit fullscreen mode

The problem with this approach is that when we decide to write to the property, we either use the __set magic method or expressively create a method to mutate the variable, which might cause a break in the codebase down the line.

Property hooks allow developers to immediately create a set directive after creating the property.

class Car{
  public string $model{
    set (string $value) {
      if(strlen($value) === 0){
        throw new ValueError("Model name cannot be empty");
      }
      $this->model = $value;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Note that the value passed to the set directive must be the same type as the property, or else an error will be thrown.

You can pass another type to the set directive and convert it to the correct type before writing to the property, as seen below:

class Car{
  public string $year{
    set (string|number $value) {
      $year = intval($value);
      if($year < 2000){
        throw new ValueError("We only accept cars produced in year 2000 and above");
      }
      $this->year= $value;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The example above shows how we can safely receive a compound type variable from the set directive and parse it to the correct type defined by the property.

You can omit the argument passed to the set directive if it is the same as the property type. For example, the two methods below are valid and behave similarly.

// --------------------------METHOD 1----------------------------
public string $model{
    set (string $value) {
      if(strlen($value) === 0){
        throw new ValueError("Model name cannot be empty");
      }
      $this->model = $value;
    }
 }

// --------------------------METHOD 2----------------------------
public string $model{
    set {
      if(strlen($value) === 0){
        throw new ValueError("Model name cannot be empty");
      }
      $this->model = $value;
    }
  }
Enter fullscreen mode Exit fullscreen mode

Note that the argument defaults to $value if it is omitted. This syntax is common in programming languages like Kotlin and C#.

Instantiating Classes Without Extra Parentheses

Before this feature, accessing members of a class in PHP involved adding extra parentheses around the class.

class Car {

  public function getName(){
    return "Toyota Camry";
  }
}

$carName = (new Car())->getName();
Enter fullscreen mode Exit fullscreen mode

If you do not wrap the new Car() call in parentheses, you will get a parse error.

The new syntax allows us to access methods, properties, and constants without the need for extra parentheses.

class Car {
  const TYPE = "vehicle";

  public function getName(){
    return "Toyota Camry";
  }
}

$carName = new Car()->getName();
echo new Car()::TYPE;
Enter fullscreen mode Exit fullscreen mode

For a full breakdown of this proposed change, check out the details in the RFC.

Introducing New Array Functions

New helper functions are coming to PHP 8.4.

Some of these functions already have their implementation in Laravel Arr or Collection helpers.

The array_find_key() Function

The array_find_key($array, $callback) function returns the key of the first element for which the $callback method returns true. If no element meets the condition, the function returns null.

function array_find_key($array, $callback) {
    foreach ($array as $key => $value) {
        if ($callback($value)) {
            return $key;
        }
    }
    return null;
}

// Example array
$numbers = [1, 2, 3, 4, 5];

// Callback to find the key of the first element greater than 3
$key = array_find_key($numbers, function($value) {
    return $value > 3;
});

echo $key;  // Output: 3 (the key of the value 4)
Enter fullscreen mode Exit fullscreen mode

The array_find() Function

The array_find_key() function is designed to search through an array and return the key of the first element that satisfies a condition defined by a callback function.

Similarly to the array_find_key(), it returns null if no matching element is found.

// Sample array
$fruits = [
    'apple' => 5,
    'banana' => 12,
    'cherry' => 8,
    'date' => 15
];

// Find the key of the first fruit with quantity greater than 10
$key = array_find_key($fruits, function($value) {
    return $value > 10;
});

echo $key;  // Output: banana
Enter fullscreen mode Exit fullscreen mode

If no fruit in the array had a quantity greater than 10, the function would return null.

The array_any() Function

The array_any() function determines if at least one element within an array fulfills a specific criterion specified by a provided evaluation function.

If at least one element meets the condition, the function returns true; otherwise, it returns false.

// Sample array
$numbers = [3, 8, 12, 5];

// Check if there is any number greater than 10
$result = array_any($numbers, function($value) {
    return $value > 10;
});

echo $result ? 'True' : 'False';  // Output: True
Enter fullscreen mode Exit fullscreen mode

If no number in the array is greater than 10, the function will return false.

The array_all() Function

The array_all() function checks if every single item in an array passes a specific test. It applies a special rule (the callback function) to each item.

If all items pass the test according to the rule, then array_all() returns true.

<?php

$numbers = [2, 4, 6, 8, 10];

// Check if all numbers are even
$areAllEven = array_all($numbers, function ($number) {
    return $number % 2 === 0;
});

if ($areAllEven) {
    echo "All numbers are even.\n";
} else {
    echo "Not all numbers are even.\n";
}

?>
Enter fullscreen mode Exit fullscreen mode

In this example, the array_all() function will iterate through the $numbers array and apply the callback function to each element. The callback checks if the number is divisible by 2 (i.e., even).

Since all numbers in the array are even, the array_all() function will return true, and the message "All numbers are even." will be displayed.


We’ve examined the key improvements introduced in PHP 8.4. These updates offer valuable enhancements for developers, including powerful new features and potential gains in efficiency.

To dive deeper into all the updates, including examples and detailed explanations, visit the official PHP 8.4.0 Release Announcement page.

Don’t forget to review the deprecations and backward compatibility changes to ensure a smooth transition to the latest version.

What’s Next? 🫡

  • If you enjoyed the article, don’t forget to share it with others.

  • I’d love to hear your thoughts—drop a comment below and let’s keep the conversation going. Cheers! 🎉

Follow me for more PHP, Node.js, TypeScript, and PHP articles! You can also find me on Twitter or LinkedIn.

Top comments (0)