Upgrading Laravel to PHP 8.0: A Comprehensive Guide for Seamless Transition

Alperen Keşkekoğlu
Insider Engineering
5 min readDec 25, 2023

In the ever-evolving landscape of web development, staying current with technology is key to unlocking new possibilities and ensuring the optimal performance of your projects. One significant stride in this journey is the upgrade from PHP 7.4 to PHP 8.0. This move brings forth a host of advantages, including performance boosts, new language features, and improved syntax. In this blog post, we’ll explore the compelling reasons to make the leap, examining the enhancements that PHP 8.0 offers and how they can positively impact your codebase and development workflow.

Why?

Updating your project from PHP 7.4 to PHP 8.0 can bring several benefits, including performance improvements, new features, and enhanced language capabilities. Here are some reasons why you might consider upgrading:

Just-In-Time (JIT) Compilation
PHP 8.0 introduces JIT compilation, which can further enhance performance by dynamically translating PHP bytecode into machine code. This feature can be particularly beneficial for CPU-bound applications.

New Features and Syntax Enhancements
PHP 8.0 introduces several new features and syntax enhancements, such as union types, named arguments, attributes, and more. These additions can improve code readability and maintainability.

Nullsafe Operator
PHP 8.0 introduces the nullsafe operator (?->), which provides a concise way to perform actions on nested objects without having to explicitly check for null at each level.

Match Expression
PHP 8.0 introduces the match expression, which is a more powerful and concise replacement for the switch statement. It allows for more expressive and readable code.

Constructor Property Promotion
PHP 8.0 introduces constructor property promotion, a feature that simplifies the process of declaring and initializing class properties in the constructor.

Error Handling Improvements
PHP 8.0 introduces improvements to error handling, including the new throw statement, which allows you to throw exceptions in a more concise and readable manner.

Consistent Behavior
Upgrading to PHP 8.0 ensures that your project benefits from the latest bug fixes and improvements, providing a more stable and reliable runtime environment.

Security Updates
Using the latest version of PHP is essential for security reasons. The PHP development community actively addresses security vulnerabilities in each release, so staying up-to-date helps keep your project secure.

Before updating, it’s crucial to thoroughly test your application with the new PHP version to ensure compatibility and address any potential issues. Additionally, check for deprecated features in PHP 7.4 that might have been removed in PHP 8.0 and update your code accordingly. Always refer to the official PHP documentation and release notes for a comprehensive understanding of the changes and improvements introduced in each version.

How?

Step 1: Update Docker Compose Configuration

In this step, you’ll update the Docker Compose configuration to use PHP 8.0. Open the docker-compose.yml file in the root directory of your Laravel project. Locate the php service within the file:

services:
php:
image: php:7.4-fpm
# ...

Update the image field to use PHP 8.0:

services:
php:
image: php:8.0-fpm
# ...

Additionally, you might want to check and update other services or configurations if they depend on specific PHP extensions or settings. For example, if you are using Nginx as a web server, make sure its configuration is compatible with PHP 8.0.

Save the changes to the docker-compose.yml file.

Then, run the following commands to update your project dependencies:

docker-compose run --rm php composer install

Step 2: Update Laravel Framework

Start by checking your Laravel project’s dependencies. Open a terminal in your project’s root directory and run:

composer show laravel/framework | grep versions

Ensure that the Laravel framework is compatible with PHP 8.0. If not, update the Laravel framework version in your composer.json file:

"laravel/framework”:”8.0"

Run the following commands to update your project dependencies:

docker-compose run --rm php composer update

Step 3: Fix Issues

Using Rector is an excellent approach to automate the process of fixing issues and making your codebase compatible with the newer PHP version. It is a powerful PHP code refactoring tool that can automatically apply various transformations to your code.

Rector can automate many changes, but there might be specific issues that require manual intervention. Review your codebase for any remaining issues and fix them accordingly.

Step 4: Verify and Test Changes

One invaluable tool for this task is PHPStan, a powerful static analysis tool that helps identify potential issues and errors in your codebase. PHPStan offers a comprehensive approach to analyzing your code, providing insights that go beyond what traditional testing might uncover. It operates statically, meaning it doesn’t require the code to be executed, allowing for quick and efficient detection of bugs. (Rector already incorporates PHPStan)

PHPUnit is a testing framework for PHP, and it’s commonly used in Laravel projects. Run your PHPUnit tests to ensure that your application’s functionality is not affected by the changes. Review the test results. If any tests fail, investigate and fix the issues. Running tests ensures that your application behaves as expected and helps catch regressions introduced during the upgrade process.

Step 5: Deploy to Staging

Deploy your updated Laravel application to a staging environment and conduct thorough testing to identify and resolve any issues that may have been missed in the local testing phase.

Step 6: Deploy to Production

Once testing is successful in the staging environment, deploy the updated Laravel application to your production server.

Step 7: Monitor

Monitor your application in the production environment, keep an eye on error logs, and address any issues promptly. Laravel’s robust error-handling tools can assist in debugging.

Conclusion

Upgrading your Laravel project to PHP 8.0 is more than a technical necessity; it’s an investment in the future of your application. By following these steps, embracing collaboration, and leveraging powerful tools, you position your Laravel project for continued success in the ever-evolving landscape of PHP development.

Happy coding!

If you are about to make a decision about your career, please take a look at Optimize Your Laravel Application’s Performance: Effective Strategies for Implementing Job Retries post by Dmitriy Kotikov, Software Engineer at Insider.

Also, don’t forget to follow the Insider Engineering Blog to see more articles ✍️

You can reach me on LinkedIn 📥

--

--