Junior — Middle
Can you give an example of the latest updates and new features added to PHP?
sobes.tech AI
Answer from AI
Recent versions of PHP have introduced several important features and improvements:
- PHP 8.1 introduces enumerations (Enums), allowing the creation of sets of named constants with type safety.
- Readonly properties have been added, which can only be initialized once.
- Fibers, primitives for cooperative multitasking, have been introduced to enable writing asynchronous code.
- Performance has been improved and type support has been extended, for example, with the addition of the "never" type.
- PHP 8.2 features readonly classes, improvements in deprecation notices, and new functions such as
random_*for generating random numbers.
Example of using enumerations in PHP 8.1:
enum Status: string {
case Draft = 'draft';
case Published = 'published';
case Archived = 'archived';
}
function publish(Status $status) {
if ($status === Status::Draft) {
// publication logic
}
}
publish(Status::Draft);