deioncube.xyz


Go Back

11/26/24

New php8.4 was released at 21 nov 2024


New php8.4 was released at 21 nov 2024 with VC17 (2022) code compiler.


Some new features in PHP 8.4:


1. Property Hooks

Property hooks allow custom logic for property access directly within the property definition.

php
class User { public string $name { get => $this->name; set => $this->name = strtoupper($value); } } $user = new User(); $user->name = 'john'; // Automatically converts to uppercase echo $user->name; // Output: JOHN

2. Asymmetric Visibility

Asymmetric visibility lets you define different visibility for reading and writing properties.

php
class BankAccount { public float $balance { public get; private set; } public function deposit(float $amount): void { $this->balance += $amount; } } $account = new BankAccount(); $account->deposit(100.00); echo $account->balance; // Output: 100.00 // $account->balance = 50.00; // Error: Cannot write to private property

3. Class Instantiation Without Parentheses

If a class doesn’t require constructor arguments, parentheses are now optional.

php
class MyClass {} $object = new MyClass; // No parentheses needed var_dump($object instanceof MyClass); // Output: bool(true)

4. New Array Functions

These functions simplify common array operations.

php
$data = [1, 2, 3, 4, 5]; // Find the first value greater than 3 $result = array_find($data, fn($x) => $x > 3); echo $result; // Output: 4 // Check if any value is greater than 4 $result = array_any($data, fn($x) => $x > 4); var_dump($result); // Output: bool(true) // Check if all values are greater than 0 $result = array_all($data, fn($x) => $x > 0); var_dump($result); // Output: bool(true)

5. New Multibyte String Functions

Work with multibyte strings more effectively.

php
$text = "ápple"; // Capitalize the first character echo mb_ucfirst($text); // Output: Ápple // Lowercase the first character echo mb_lcfirst("APPLE"); // Output: aPPLE // Trim multibyte characters echo mb_trim(" ápple "); // Output: ápple

6. Deprecated Attribute

Mark code elements as deprecated.

php
class MyLibrary { #[Deprecated("Use newMethod() instead.")] public function oldMethod() { echo "This method is deprecated."; } public function newMethod() { echo "This is the new method."; } }

7. HTML5 Support with DomHTMLDocument

Enhanced HTML parsing with the new DomHTMLDocument class.

php
$html = << Example

Hello World

HTML
; $doc = new DomHTMLDocument(); $doc->loadHTML($html); echo $doc->getElementsByTagName('p')->item(0)->textContent; // Output: Hello World

New php8.4 was released at 21 nov 2024


Written by: php
Share this news: