What’s new in PHP 8

PHP 8, released in November 2021, has several new features and improvements including:

  1. JIT (Just-In-Time) Compilation
  2. Union Types
  3. Named Arguments
  4. Match Expression
  5. Improved Type Annotations
  6. Deprecation of old Features
  7. Attributes (Syntax)
  8. Improved Error Handling and Debugging.

 

JIT (Just-In-Time) Compilation:

Just-In-Time (JIT) Compilation is a feature in PHP 8 that enhances the performance of the language by compiling PHP code into machine code at runtime. This results in faster execution times, as the compiled code is executed directly by the processor. JIT helps to optimize the frequently executed parts of code, improving the overall performance of the PHP application. JIT is enabled by default in PHP 8, but it can also be turned off if necessary.

 

Union Types

Union Types in PHP 8 allow developers to specify that a parameter or return type can accept multiple types, represented as a union of types. This allows for more flexible type checking and better type safety in PHP code. Union Types are declared by using the pipe symbol (|) to separate the different types, for example:

In this example, the combine the function takes two parameters that can be either a string or an integer and returns a value that can be either a string or an integer.

 

Named Arguments

Named Arguments in PHP 8 allows developers to specify arguments for a function call by name, instead of by position. This provides greater clarity and self-documentation to the code and makes it easier to maintain. Named Arguments can also be used to set default values for arguments that are not passed.

Here’s an example of a function that uses Named Arguments:

In this example, the createRectangle the function takes three arguments, but the values are passed by name using the colon syntax, rather than by position. The filled the argument has a default value of false, so it can be omitted in the function call if desired.

 

Improved Error Handling and Debugging

Improved Error Handling and Debugging in PHP 8 aims to make it easier for developers to debug and fix issues in their code. PHP 8 includes several improvements in this area, such as:

  1. Better error messages: PHP 8 generates more detailed error messages with stack traces that are easier to understand.
  2. Consistent error handling: PHP 8 standardizes the way that errors and exceptions are handled, making it easier to write and maintain code.
  3. Type Errors: PHP 8 now reports type errors as exceptions, making it easier to catch and debug type-related issues in the code.
  4. New Exception classes: PHP 8 introduces several new exception classes for specific error scenarios, such as DivisionByZeroError and ParseError.

By making these changes, PHP 8 aims to make it easier for developers to write robust and reliable code, and to quickly identify and fix issues when they arise.

 

Match Expression

The match expression in PHP 8 is a new way of performing conditional branching in the language. It provides a concise and readable syntax for handling multiple conditions in a switch-case-like structure. The match expression evaluates an expression and branches to the first case whose value matches the result of the expression.

Here’s an example of using a match expression:

In this example, the match expression takes the value of $value and branches to the first case whose value matches the result of the expression. The matching is done using a strict equality comparison (===). The default the case is used to handle any values that don’t match any of the other cases.

Improved Type Annotations

Improved Type Annotations in PHP 8 aims to make it easier for developers to declare the types of variables, function parameters, and return values. PHP 8 introduces several improvements in this area, such as:

  1. Better Type Inference: PHP 8 now has improved type inference, meaning that it can more accurately determine the type of a value in certain cases. This makes it easier to write correct type annotations.
  2. Nullable Types: PHP 8 now allows developers to declare that a value can be either a specific type or null, using the syntax ?Type (e.g. ?int).
  3. Coercive Types: PHP 8 introduces coercive types, which allows developers to declare that a type should be automatically cast to a specific type if it can be safely cast.
  4. Improved Type Variance: PHP 8 now allows developers to declare the type variance (e.g. whether a type is covariant or contravariant) of functions and classes, making it easier to write correct code.

By making these changes, PHP 8 aims to make it easier for developers to write correct and self-documenting code, and to catch type-related issues before they cause problems at runtime.

 

Deprecation of old Features

Deprecation of old Features in PHP 8 involves removing support for older, less used or outdated features in the language. The goal of this is to simplify the language, reduce code complexity, and improve performance.

Some of the features that have been deprecated in PHP 8 include:

  1. ASP-style <% tags: PHP 8 no longer supports the use of ASP-style tags (<%) in PHP code, as these are less commonly used than the <?php tags.
  2. Deprecation of create_function(): PHP 8 deprecates the create_function() function, which creates an anonymous function dynamically at runtime. This feature is not widely used and has been replaced by anonymous functions in modern PHP code.
  3. Deprecation of each(): PHP 8 deprecates the each() function, which returns the current key/value pair from an array and advances the internal array pointer. This feature has been replaced by the foreach loop.

By removing support for these older features, PHP 8 aims to simplify the language and make it easier for developers to write correct and efficient code. It also encourages developers to use more modern and recommended practices in their code.

 

Attributes (Syntax)

Attributes in PHP 8 is a new syntax for declaring metadata about code elements such as classes, methods, properties, and parameters. Attributes are specified using an @ symbol followed by the attribute name, and can be used to add information or behavior to code elements.

Here’s an example of using an attribute in PHP 8:

In this example, the @Route attribute is used to specify the URL for the show method. This information can be used by a routing library to map URLs to the correct methods in the application.

Attributes in PHP 8 are similar to annotations in other programming languages and provide a way to add metadata to code elements in a structured and standardized way. They can be used to add information for code analysis, documentation generation, or other purposes.