PHP : 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
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.
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 Animal{ public private(set) string $name; public setName(string $foo){ $ths->name = $foo; } } $animal = new Animal(); echo $animal->name; // This will run correctly
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 function __construct(public string $model) { } }
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 $model{ set (string $value) { if(strlen($value) === 0){ throw new ValueError("Model name cannot be empty"); } $this->model = $value; } } }
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.
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; } } }
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.
// --------------------------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; } }
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 { public function getName(){ return "Toyota Camry"; } } $carName = (new Car())->getName();
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.
class Animal{ public private(set) string $name; public setName(string $foo){ $ths->name = $foo; } } $animal = new Animal(); echo $animal->name; // This will run correctly
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.
class Car { public function __construct(public string $model) { } }
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.
class Car{ public string $model{ set (string $value) { if(strlen($value) === 0){ throw new ValueError("Model name cannot be empty"); } $this->model = $value; } } }
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.
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; } } }
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.
The above is the detailed content of PHP : Top Features and Improvements. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
