check old php scripts for upgradeing/migrating to newer php version - php

I've many PHP-Scripts based on older PHP Versions. I'm currently using PHP 5.3 and want to upgrade to a newer Version, but I don't know which is compatible with the functions I'm using. Is there a Framework or any other tool to check the Scripts with? I want to upgrade/migrate to PHP 7.2 and I know that some functions (like mysql) are removed.
I don't remember every function I used in those hundreds of Scripts and don't really want to search line for line for deprecated and removed functions.

You can have a look at Phan. It runs under PHP and can statically analyze your scripts for compatibility with newer PHP versions.
If you call it using
phan --directory <directory_with_scripts> --target-php-version 7.2
It'll tell you what will fly and what will not!
Hope this helps!

Related

Which PHP Version To Use While Migrating from 5.3?

I am working on a project which is in php 5.3+. I need to create a separate Api which is to be deployed on same server for which i am using the Symfony2.
As i need to deploy new api on same server i want to use php 5.5+ version Now i need to upgrade my old project to new version of php and mysql as the server currently is on 5,3+. I tried the php 5.4+ and mysql 5.5+ on my local system the project works fine, but when i switched to php 5.5+ it shows me error that states mysql_connect is Deprecated.
I checked on several sites and came across a solution where i can skip the deprecated message on php 5.5 + via doing some ini settings.
Now my main concern is whether is it a right solution to migrate to php5.5+ and start new feature development out there As i need to use 5.5+ so that i wont face same deprecated issue with any other thing in future in new Apior should i migrate to 5.4+.
Kindly Reply
Upgrade to the latest 5.x branch. Do not upgrade to 7.
A new major version, means that backwards compatibility is broken. You can expected that some deprecated features are dropped. In general upgrading to a new mayor PHP version requires you to upgrade your code base.
When upgrading to a new minor version, you can expect your application to keep working. Typically new functionality is added and some functionality is deprecated, but nothing is removed.
Deprecation is a notification to indicate that functionality might be removed in an upcoming version.
The notifications are meant for you as developer, to know what you need to change to make your code ready for upgrading new major version.
On the production environment you should disable these notifications through error_reporting.
The deprecation notifications should not keep you from upgrading to a new minor version. Without this upgrade it's difficult to know what to change to make your software up to date.
Conclusion: Update to PHP 5.6. Than, when you're ready and have time, change / update your code so you're not using deprecated features. When that's done, you can upgrade to PHP 7.
The question I would ask myself if I were you is how long I would want to maintain a code base with deprecated features... my suggestion is to migrate to PHP 7 (or 5.6 minimum), upgrade projects and start new feature developments... will pay better on the long run.
The short answer is "Move to PHP 5.6 now; then move to PHP 7 later, once you've dealt (or as part of the exercise of dealing with) with all the issues that would break your code in PHP 7.
The changes from PHP 5.3 to 5.6 are not really that big. The really big changes happened between 5.2 and 5.3, but if you're on 5.3 already then you've already dealt with that. Moving up to 5.6 from there is virtually seamless.
Yes, there are some backward-compatibility breaks between every version, and yes, you must read the upgrade notes for each version before you start, but in truth, 5.4, 5.5 and 5.6 were more about adding features and increasing performance than they were about breaking things.
You specifically mention the mysql extension being deprecated in 5.5. This is true, and you will get warnings. Note, however, that these functions will still work in 5.5 and 5.6. The deprecation warning is telling you that they are considered obsolete and will be removed in the future. This has now happened in 7.0. These warnings are basically there to give you fair warning; to give you time to change your code before the next upgrade cycle. Your code will still work for now if you ignore the warnings, but you should start work immediately to fix the problem.
Fixing the problem, in the case of the mysql extension, means replacing your database code so that it uses either the mysqli or PDO extensions.
The mysqli exension is basically a drop-in replacement for the old mysql extension. There are some changes (it adds an OO interface if you want to use it, and it doesn't use a single global connection object as the old library did, so you need to pass the connection object into every call), but it's generally fairly easy to convert from one to the other.
PDO is a more advanced database library which can support a range of different database types including Mysql. It is quite different to the old extension, so converting is a more complex process. But if you can use this one, it's generally considered the preferred option.

Will php -l checking work on PHP files of older/past and future PHP versions?

For example I have a PHP 5.2 php.exe which I'm using to run the syntax checks of several PHP files - this works fine (have not had any problems) - but this may be because the files which I checked were PHP 5.2 files...
So I was wondering what would happen if I checked the syntax of lets say functional PHP 4 files or PHP 5.2+ files - in all instances would the check be fine or produce errors?
I would imagine checking pre PHP 5.2 files (like PHP 4 etc) would be fine (as PHP is obviously backwards compatible and I'm assuming php - l does not take function names into account when checking; so deprecated functions won't be an issue)...but I can see that the checking of PHP 5.2+ files could be an issue though, would I be correct in saying that; it would work fine on PHP files of older/past PHP versions but not future?
It's impossible to say with any certainty that a PHP 4 file will work with PHP 5.2 or that a PHP 5.4 file will work with PHP 5.2. There have been many changes between the versions.
From newer version to older version
If you have a script written in 5.4 you will have no problems with that script in 5.2 IF and ONLY IF the script doesn't include any new functions introduced in 5.3 or 5.4.
From older version to newer version
If you have a script written in PHP 4.x and you want to use it in PHP 5.2 for the most part there won't be issues but there are some backward incompatible changes.
Here are the migration guides for you:
Migrating from PHP 4 to PHP 5.0.x
Migrating from PHP 5.0.x to PHP 5.1.x
Migrating from PHP 5.1.x to PHP 5.2.x
Migrating from PHP 5.3.x to PHP 5.4.x
Migrating from PHP 5.4.x to PHP 5.5.x

Is PHP 5.3 backwards-compatible with PHP 5.2?

I'm starting to learn PHP. I would like to use PHP 5.3 because it's the newest version so far. I'm working on a project in which I'll use PHP 5.2 scripts that someone gave me.
Are they going to run just fine even though I'm using PHP 5.3?
PHP.net features a guide for upgrading from PHP 5.2.x to PHP 5.3. This includes a section on backwards compatibility.
From my experience, the transition from 5.2 to 5.3 went pretty smoothly smoothly. The only problems I had with my app was to ensure my DateTime setting were property configured in my php.ini, and filter out some overly-agressive deprecation warnings that started showing up.
No, 5.3 is not backward compatible, and by all means it shouldn't be seen as a minor version upgrade.
There is a page dedicated to incompatible changes: http://www.php.net/manual/en/migration53.incompatible.php
Also consider functions you have designed but have been accepted into 5.3. Since php doesn't support overloading or overwriting of functions, this will give an error.
For the most part, minor version releases aren't going to cause you problems. You should be okay.
Obviously, if you do have any problems, you'll want to make sure you have error_reporting set to see all errors. PHP: error_reporting
Also, check the manual for changes in 5.3 from 5.2: Migrating from PHP 5.2.x to PHP 5.3.x
There are some minor issues. Call time pass by reference are deprecated. There are new reserved words (ie. goto).
php has no backward compatibility in all version. It often removed some functions when it changed version. The problem occurs when you have to upgrade server and need to change php version but some php scripts no longer work with new php version. You also have to correct php script which is a bigger job. But if the script belong to other users rather than yours, you can't upgrade those scripts. This is a real headache situation that I'm fed up with php and switch to perl instead for backward compatibility reason.

PHP 5.2.x and 5.3.x

What are the differences between PHP 5.2.x and 5.3.x?
Because all extensions are made separately for these.
Thanks a lot.
PHP 5.3 is a newer version with a number of new features (closures, namespaces, etc.). Is is the current stable version for the PHP project. PHP 5.2 is an older version, which is now heading towards the end of its support cycle - now only important security issues are being fixed. If you start a new project, start it with PHP 5.3.
When changes in PHP core are made that change the structure enough that extensions can no longer be compatible - the second digit in the version number is incremented (5.0, 5.1, 5.2, 5.3). The first digit is for very major changes changing the whole language (like between PHP 4 and 5). Between 5.2 and 5.3, you can expect source code compatibility (though some incompatibilities exist) but not binary module compatibility.
You can find an exhaustive list of backwards incompatible changes from 5.2 to 5.3 on PHP.net.
PHP extensions depends on the phpapi version among other things, along with the Zend Module API version and the Zend Extension API version, which as far as I'm aware, changes at each PHP version because of the core changes made to the language. This explains why each PHP version has a new build for each extension.
You can find the answer in PHP official website.
In this page: Migrating from PHP 5.2.x to PHP 5.3.x.

Testing against various PHP versions

I have a PHP app that I would like to test against various PHP versions. I don't need to configure it with Apache or mysql, a simple php command line binary would do. I would however need cURL with SSL support compiled in.
The reason I ask as I tried compiling my own and ran into all sorts of issues with newer versions of curl don't work with older versions of PHP etc.
I already have a test suite and would like to run it against as many versions of PHP as possible.
Is there a good way to do this short of manually compiling a ton of php binaries?
Check phpfarm, primarily developed for PEAR's CI machine.

Categories