Modernizing a PHP codebase from PHP v5.4 to v8.0+

Modernizing a PHP codebase from PHP v5.4 to v8.0+

Tags
PHP

I’m currently upgrading a large codebase to PHP 8.0. Here’s some documentation from the process to refactor the codebase to use a modern PHP approach.

Using array_key_exists() on objects is deprecated.

array_key_exists no longer supports checking for properties defined within an object... instead use property_exists

Note: The arguments are swapped.

Each() deprecated

To replace each () deprecations you just use a foreach() loop: while (list ($key, $val) = each ($_POST['selected'])) { becomes foreach ($_POST['selected'] as $key => $val) {

Required parameter $x follows optional parameter $y

PHP wants you to put required arguments first as parameters before optional arguments when defining a function’s argument list.

MYSQL → MSQLI Port

mysql_fetch_arraymysqli_fetch_array
mysql_num_rowsmysqli_num_rows
mysql_num_fieldsmysqli_num_fields
mysql_fetch_rowmysqli_fetch_row
mysql_real_escape_stringmysqli_real_escape_string
mysql_fetch_assocmysqli_fetch_assoc
mysql_querymysqli_query
mysql_insert_idmysqli_insert_id
mysql_field_name → no replacement function, must be custom written, see https://stackoverflow.com/questions/14629636/mysql-field-name-to-the-new-mysqli
mysql_connectmysqli_connect
mysql_select_dbmysqli_select_db
Remove instances of get_magic_quotes_gpc (separate commit!)
mysqli_query arguments are reversed. Go through each instance and reverse them!
mysql_closemysqli_close
mysql_free_result

Resources