Logo

dev-resources.site

for different kinds of informations.

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

Published at
12/28/2024
Categories
php
web
development
webdev
Author
Mhammed Talhaouy
Categories
4 categories in total
php
open
web
open
development
open
webdev
open
๐Ÿš€ ๐—ฃ๐—›๐—ฃ ๐Ÿด.๐Ÿฐ ๐—ถ๐˜€ ๐—›๐—ฒ๐—ฟ๐—ฒ: ๐——๐—ถ๐˜€๐—ฐ๐—ผ๐˜ƒ๐—ฒ๐—ฟ ๐—ช๐—ต๐—ฎ๐˜'๐˜€ ๐—ก๐—ฒ๐˜„ ๐˜„๐—ถ๐˜๐—ต ๐—˜๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ๐˜€!

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 $hashedPassword;

    public string $password
    {
        get => '***'; // Masked password for display
        set (string $plainPassword) {
            $this->hashedPassword = password_hash($plainPassword, PASSWORD_DEFAULT); // Hash the password
        }
    }

    public function verifyPassword(string $plainPassword): bool
    {
        return password_verify($plainPassword, $this->hashedPassword);
    }
}

// Example usage
$user = new User();

// Set the password
$user->password = 'secure123';

// Access the password (masked)
echo $user->password; // Output: ***

// Verify the password
var_dump($user->verifyPassword('secure123')); // bool(true)
var_dump($user->verifyPassword('wrongpassword')); // bool(false)

๐Ÿ”น 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)

๐Ÿ”น 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);

๐Ÿ”น 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

๐Ÿ”น 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

๐Ÿ”น 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!

Featured ones: