Running Laravel 4.2 Project on PHP 7.4

 David

This is a copy of a blog content posted by Yavuz Selim Bilgin on December 2019.
It's a very good article for those who are still using Laravel 4.2. and wishes to upgrade PHP 7.x as mcrypt extension is no longer supported in PHP 7.x, while Laravel 4.2 requires it.
I'm porting the copy of the article in case this blog post gets deleted from https://ysbilgin.medium.com/running-laravel-4-2-project-on-php-7-3-or-7-4-b18e41caa0b
I also add few additional helpful things that I found along the way while upgrading the PHP7 to make it work with Laravel 4.2 even better.


Running Laravel 4.2 Project on PHP 7.3 or 7.4


by Yavuz Selim Bilgin on Dec 1, 2019

I have some legacy Laravel 4.2 projects running on PHP 7.1, and PHP 7.1 is no longer supported. (https://twitter.com/krakjoe/status/1201010799034195968?s=12) and then I decided to upgrade them to latest PHP 7.4

Problem

Laravel 4.2 requires mcrypt extension which is deprecated in PHP 7.1 and removed in PHP 7.2.

Solution

Laravel 4.2 has a release for PHP 7, and you can install mcrypt for ≥ 7.2 by PECL.

Step 1: Update Laravel to 4.2.22

Taylor has published a release for PHP 7 compatibility. Update your Laravel to 4.2.22 https://github.com/laravel/framework/releases/tag/v4.2.22

Step 2: Check your code compatibility

First you must be sure about your Laravel 4.2 code is compatible with PHP 7.4. You can check PHP change logs.

Step 3: Disable deprecated errors (optional)

Add the line below to your app/config/app.php because you will get deprecated errors for mcrypt. It might not be a good idea but we are already trying to run some deprecated code consciously. Alternatively you can install https://github.com/tomgrohl/laravel4-php71-encrypter package.


# /app/config/app.php
error_reporting(E_ALL ^ E_DEPRECATED);

Step 4: Install and enable mcrypt extension

Install mcrypt extension on your PHP server with PECL.

$ sudo pecl install mcrypt

Step 5: Better Error Handling
PHP v7 introduces ParseError if there's any runtime exception and Laravel 4 has no PHP7 support, so it does not log/display the actual source/cause of the error. It is impossible debug and fix any problem without knowing exactly what caused the error so you either have to upgrade Laravel to 5 for PHP7 support or downgrade PHP to 5.6. However, there's a workaround for using PHP7 with Laravel 4 by disabling the Laravel error handler, which can be done by adding the following 2 lines of code in app/config/app.php right after the <?php
# /app/config/app.php
set_error_handler(null);
set_exception_handler(null);

Step 6: Test it!

Now, it might be ready but it’s better you test it again. Then you’re ready to run your legacy Laravel 4.2 code on latest PHP 7.4 and get all updates including security ones.
Go Back to List Page

Leave a comment

Name : Comment : view emoticons
Please consider signing up for our website.
If you sign up and log in:
  •   You can avoid the "I'm not a robot" captcha when commenting
  •   You can also avoid typing your name every time
  •   You can upload a picture for each comment
  •   You can change or delete your comment within 1 hour
  •   You can track all the comments you posted on this site
  •   You can read blog posts that are only open to members
  •   You can look up blogs using the search feature
  •   More privileges for our friends & families coming...

OK, Sign me up!

Emoticons are a great way to visually express how you feel.
However, there are times when unintended content is converted to emoticon because the content happens to have one of the emoticon symbols. That's why it's always good idea to preview your comment before posting and when you see this type of problem, you can indicate NOT to auto convert.