I’ve been out of the loop with PHP development for a while and recently started getting into more projects and development with my work, I was surprised to see the advancements it’s made in the past few years. These are some things that I think stand out and could be used in any new project you’re undertaking.
Let’s take a look at what new features are available to us, that we can use right now in PHP8, with a few examples.
JIT (Just-In-Time) Compilation
One of the most anticipated features in PHP 8 is the Just-In-Time (JIT) compiler. JIT offers a major performance boost by compiling parts of the code at runtime, rather than interpreting it line by line. This means that PHP can now run faster and handle more complex operations, making it more competitive with other programming languages in terms of speed. While not every application will see a drastic performance increase, applications that are computation-heavy will benefit significantly.
Union Types
PHP 8 introduces union types, allowing developers to declare multiple types for a single parameter or return value. This means you can now specify that a function might return an int
or a string
, providing greater flexibility and reducing the need for complex workarounds. This addition makes PHP code more expressive and easier to understand.
function foo(int|string $input): int|string {
// implementation
}
Named Arguments
Named arguments in PHP 8 allow you to pass arguments to a function based on the parameter name, rather than its position. This can greatly improve code readability, especially when dealing with functions that have multiple optional parameters. It also helps in avoiding errors related to the order of parameters.
function sendEmail(string $to, string $subject, string $body, bool $html = false) {
// implementation
}
// With named arguments
sendEmail(
to: '[email protected]',
subject: 'Welcome!',
body: 'Thank you for joining us.',
html: true
);
Attributes (Annotations)
PHP 8 introduces a native way to add metadata to classes, methods, properties, and function parameters using attributes, also known as annotations in other languages. This allows for cleaner and more maintainable code by replacing the need for PHPDoc comments or external libraries to handle metadata.
#[Route("/home", methods: ["GET"])]
function home() {
// implementation
}
Match Expression
The match
expression is a more powerful and flexible version of switch
. Unlike switch
, match
expressions can return values, do not require break statements, and support strict comparisons by default. This leads to more concise and less error-prone code.
$status = match($statusCode) {
200 => 'OK',
404 => 'Not Found',
500 => 'Internal Server Error',
default => 'Unknown status',
};
Constructor Property Promotion
PHP 8 simplifies class property declarations with constructor property promotion. This feature allows you to declare and initialize properties directly in the constructor parameters, reducing boilerplate code and making classes more concise.
class User {
public function __construct(
private string $name,
private int $age,
) {}
}
Nullsafe Operator
The nullsafe operator (?->
) allows you to safely navigate through a chain of objects without having to explicitly check for null
at each step. This is particularly useful for working with deeply nested objects or optional data.
$username = $user?->getProfile()?->getUsername();
Now, that’s a lot to take in. Especially if you’re used to writing code one way, like I am. I still refer to other articles and documentation on how to use these new features and sometimes look through code I’ve written to see if it can be refactored using any of the new and efficient features.
Personally, I think the class constructor property promotion is going to clean up a lot of my custom classes and I am excited to use the attribute metadata in some of my projects.