DEV Community

Cover image for ๐Ÿš€ ๐—ฃ๐—›๐—ฃ ๐Ÿด.๐Ÿฐ ๐—ถ๐˜€ ๐—›๐—ฒ๐—ฟ๐—ฒ: ๐——๐—ถ๐˜€๐—ฐ๐—ผ๐˜ƒ๐—ฒ๐—ฟ ๐—ช๐—ต๐—ฎ๐˜'๐˜€ ๐—ก๐—ฒ๐˜„ ๐˜„๐—ถ๐˜๐—ต ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ๐˜€!
Mhammed Talhaouy
Mhammed Talhaouy

Posted on

๐Ÿš€ ๐—ฃ๐—›๐—ฃ ๐Ÿด.๐Ÿฐ ๐—ถ๐˜€ ๐—›๐—ฒ๐—ฟ๐—ฒ: ๐——๐—ถ๐˜€๐—ฐ๐—ผ๐˜ƒ๐—ฒ๐—ฟ ๐—ช๐—ต๐—ฎ๐˜'๐˜€ ๐—ก๐—ฒ๐˜„ ๐˜„๐—ถ๐˜๐—ต ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ๐˜€!

The latest release of PHP, PHP 8.4, is packed with features that simplify code, improve performance, and align with modern development practices. Hereโ€™s a quick look at the highlights, along with examples:

๐Ÿ”น Property Hooks

Eliminate boilerplate getters and setters! Customize property access behavior directly in your classes.

class User {
    private string $password;

    public function __get(string $name) {
        if ($name === 'password') {
            return '***';
        }
        throw new Exception("Property $name not found");
    }

    public function __set(string $name, $value) {
        if ($name === 'password') {
            $this->password = password_hash($value, PASSWORD_DEFAULT);
        }
    }
}

$user = new User();
$user->password = 'secure123';
echo $user->password; // Output: ***
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Asymmetric Visibility

Control reading and writing access with different visibility levels for properties.

class BankAccount {
    public function __construct(
        public readonly float $balance = 0.0
    ) {}

    private function setBalance(float $amount): void {
        // Business logic for updating the balance
    }
}

$account = new BankAccount(100.0);
echo $account->balance; // Public for reading
// $account->balance = 200.0; // Error: Cannot modify (private for writing)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น New Array Find Functions

Work smarter with arrays using new utility functions like array_find and array_all.

$users = [
    ['id' => 1, 'name' => 'Alice'],
    ['id' => 2, 'name' => 'Bob'],
];

$user = array_find($users, fn($user) => $user['name'] === 'Bob');
print_r($user); // Output: ['id' => 2, 'name' => 'Bob']

$isAllAdults = array_all($users, fn($user) => $user['age'] >= 18);
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Class Instantiation Without Extra Parentheses

Chain methods or access properties seamlessly without redundant parentheses.

class Task {
    public function status(): string {
        return 'Completed';
    }
}

echo (new Task)->status(); // Output: Completed
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น HTML5 DOM Parser

The DOM parser now fully supports HTML5, improving web compatibility.

$dom = new DOMDocument();
$dom->loadHTML('<!DOCTYPE html><html><body><div>Hello</div></body></html>');
echo $dom->saveHTML(); // Parses HTML5 content properly
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Deprecations

Stay ahead of breaking changes:

  • Implicit nullable types are deprecated.
  • Session usage via GET/POST has been deprecated.

๐Ÿ’ก Why It Matters:

PHP 8.4 empowers developers to write cleaner, more efficient, and modern code. Whether you're building web applications, APIs, or anything in between, these features make development more enjoyable.

๐Ÿ‘‰ Which feature excites you the most? Letโ€™s discuss in the comments!

Top comments (4)

Collapse
 
speratus profile image
Andrew Luchuk

Friendly heads up, a number of the features you mention here are not new to PHP 8.4. In particular these features have existed in PHP for a while:

  • magic __get and __set methods
  • class instantiation without parenthesis
Collapse
 
tal7aouy profile image
Mhammed Talhaouy
Collapse
 
speratus profile image
Andrew Luchuk

I have read the release notes. The example of property hooks in the article just uses PHP magic methods. Those have existed for a really long time. If you look at the release notes sample of property hooks, you'll see that property hooks are not magic methods.

Here is a proper example of property hooks:

class A {
    public string $demo;

    public string $demo
    {
        set (string $newDemo) {
            $this->demo = preg_replace("/World/", "property hook", $newDemo);
        }
    }
}

$a = new A;
$a->demo = "Hello, world";
echo $a->demo; // Prints "Hello, property hook"
Enter fullscreen mode Exit fullscreen mode

Asymmetric visibility is new, but the example of it here does not illustrate asymmetric visibility. Instead, it illustrates features that have been around for a while.

An example of asymmetric visibility:

class A {
    public private(set) $demo = 'Hello World'; // Outside classes can get the property, but cannot set it.
}
Enter fullscreen mode Exit fullscreen mode

Finally, class instantiation without parentheses is not new. I've used it for years now. If you read the release notes carefully, you will see that the class instantiation without extra parentheses is actually different.

Pre PHP 8.4:

class A {
    public function method() {}
}
(new A)->method(); // the `new A` syntax has been available for some time. I don't know which version it was added in, but I have used it in PHP 7.4.
Enter fullscreen mode Exit fullscreen mode

PHP 8.4 Version:

class A {
    public function method() {}
}
new A()->method();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zeestech profile image
zees-tech

git@github.com:zees-tech/resumebuilder.git

check code and feel free to update