What’s New in PHP 8.4: A Look at the Latest Features

PHP 8.4 introduces several exciting features, improvements, and optimizations. This blog enhances usability, performance, and security, paving the way for more efficient and expressive coding. Let’s dive into the most notable features of PHP 8.4.

In this blog:

Top 10 latest features in PHP 8.4

1. Readonly classes

Building upon the readonly properties introduced in PHP 8.1, PHP 8.4 allows you to define an entire class as readonly. When a class is marked as readonly, all its properties are automatically treated as immutable, simplifying the code and reducing boilerplate.

readonly class Config
{
    public string $appName;
    public string $version;

    public function __construct(string $appName, string $version)
    {
        $this->appName = $appName;
        $this->version = $version;
    }
}

This makes configuration or data-transport objects safer and easier to work with.

2. New 'json_validate()' function

Validating JSON strings has always been difficult in PHP. PHP 8.4 introduces the json_validate() function, which checks whether a string is a valid JSON document without decoding it, saving time and resources.

$json = '{"name": "PHP", "version": 8.4}';
if (json_validate($json)) {
    echo "Valid JSON!";
} else {
    echo "Invalid JSON.";
}

This is a great addition for scenarios where you only need to ensure the JSON format is correct.

3. Improved Performance

Performance enhancements continue to be a strong focus in PHP 8.4. The JIT (Just-In-Time) compiler has received optimizations, resulting in better performance for computationally intensive tasks. Additionally, improvements in memory usage and garbage collection make PHP 8.4 more efficient than ever.

4. Typed Constants in Traits

PHP 8.4 now allows you to define typed constants in traits, improving type safety and aligning traits with classes and interfaces.

trait LoggerTrait
{
    public const string DEFAULT_LOG_LEVEL = 'info';
}

This change helps you build reusable and robust code by enforcing consistent data types in traits.

5. Enhanced 'str_starts_with()' and 'str_ends_with()'

While str_starts_with() and str_ends_with() were introduced in PHP 8.0, PHP 8.4 expands their functionality. They now support arrays, enabling you to check multiple prefixes or suffixes in one go.

$prefixes = ['http', 'https'];
$url = 'https://example.com';

if (str_starts_with($url, $prefixes)) {
    echo "Valid URL!";
}

This feature simplifies operations on strings with multiple patterns.

6. Dynamic callable syntax

PHP 8.4 simplifies the creation of dynamic callables with a new syntax. This eliminates the need for verbose anonymous functions or workarounds.

$callable = dynamic($object->method(...));
$callable();

Dynamic callables make your code more concise and readable.

7. Improvements to enum types

Enums, introduced in PHP 8.1, gain new features in PHP 8.4. Enums can now implement interfaces and have constant values, making them more powerful and versatile.

enum Status implements Loggable
{
    case Pending;
    case Approved;
    case Rejected;

    public function log(): void
    {
        echo $this->name;
    }
}

This opens up more possibilities for enums in object-oriented design.

8. Better error reporting for undefined variables

Undefined variables now throw a Notice instead of being silently ignored, improving debugging and reducing potential runtime issues.

echo $undefinedVar; // Notice: Undefined variable $undefinedVar

This is particularly useful in catching typos and logical errors during development.

9. Improved extension compatibility

PHP 8.4 focuses on compatibility, making it easier to integrate extensions and external libraries. This change benefits developers working in ecosystems that rely heavily on third-party tools.

10. Security enhancements

PHP 8.4 comes with several under-the-hood security improvements, including stricter type checks and protection against certain attack vectors. It’s another reason to upgrade and stay secure in your applications.

Why upgrade to PHP 8.4?

  1. Performance Boost: Faster execution and memory efficiency.
  2. Developer Productivity: New features like json_validate() and readonly classes simplify coding.
  3. Improved Debugging: Better error reporting reduces debugging time.
  4. Enhanced Type Safety: Typed constants and improved enums ensure more robust applications.
  5. Future-Ready: Staying updated ensures compatibility with modern tools and libraries.

How to upgrade to PHP 8.4?

  1. Check Compatibility: Ensure your current codebase and libraries support PHP 8.4.
  2. Test Extensively: Use staging environments to test your application with PHP 8.4 before deploying to production.
  3. Update Dependencies: Update composer packages and extensions to their latest versions.

PHP 8.4 improves PHP, making it faster, safer, and easier to use for web development. With its new features and enhancements, it meets the needs of developers who value simplicity, safety, and performance. Upgrade today to experience the benefits!

Atatus PHP monitoring: Enhance Your Application's Performance Effortlessly

Atatus PHP Monitoring provides a comprehensive solution for tracking and optimizing the performance of PHP applications. It is designed to ensure high availability, pinpoint issues quickly, and enhance the overall user experience.

Key Features of Atatus PHP Monitoring

  • Comprehensive Performance Monitoring: Capture all requests to your PHP applications and track performance metrics. Track critical performance metrics, including request throughput, response times, and application uptime.
  • Real-Time Performance Insights: Gain visibility into slow transactions and optimize them to improve user experience.
  • End-to-End Tracing: Visualize complete traces across your stack to identify bottlenecks, errors, and performance issues before they affect users.
  • Database Query Optimization: Monitor all SQL and NoSQL queries, pinpoint slow queries, and optimize their performance for faster response times.
  • Support for Multiple PHP Frameworks: Atatus supports popular PHP frameworks like WordPress, Magento, Laravel, CodeIgniter, CakePHP, Symfony, Yii, and Slim, making it a versatile monitoring tool for diverse platforms.

Try your 14-day free trial of Atatus.

Atatus

#1 Solution for Logs, Traces & Metrics

tick-logo APM

tick-logo Kubernetes

tick-logo Logs

tick-logo Synthetics

tick-logo RUM

tick-logo Serverless

tick-logo Security

tick-logo More

Pavithra Parthiban

Pavithra Parthiban

A technical content writer specializing in monitoring and observability tools, adept at making complex concepts easy to understand.
Chennai