Skip Navigation
Expand
  • Advanced SearchOpens new dialog
    Enter plus (+) or minus (-) signs in search terms to make a word required or excluded
    Search Tips
    Filter by product
    Filter by category
PHP 8.3 Migration Guide
Answer ID 12880   |   Last Review Date 08/19/2024

What do I need to know about migrating customization from PHP 5.6 to PHP 8.3?

Environment:
Customization
Oracle B2C Service 24C+

Resolution:
This migration guide outlines several patterns the Product Development team identified while upgrading the internal modules of the product from PHP 5.6 to PHP 8.3.

Note: This migration guide is not a holistic guide. The patterns are not limited to below ones.
 
Functions/Use Cases Description PHP 5.6 PHP 8.3
"create_function" is removed In PHP 5.6, create_function was used to create anonymous functions. This function was deprecated in PHP 7.x and removed in PHP 8.x.
To update your code, replace create_function with anonymous functions.
$func = create_function('$a,$b', 'return $a+$b;');
echo $func(1,2);
$func = function($a, $b) { return $a + $b; };
echo $func(1,2);
"each" function is removed
In PHP 5.6, the each function was used to iterate over arrays. This function was deprecated in PHP 7.x and removed in PHP 8.x.
To update your code, use modern alternatives like foreach
$array = [1,2];
while(list($key, $value) = each($array))
{ echo $value;}
$array = [1,2];
foreach($array as $key => $value) { echo $value;}
Type hinting PHP 5.6 had limited type hinting capabilities, allowing only class names and arrays. PHP 8.x enforces strict type hinting, supporting scalar types (int, float, string, bool), return types, and union types. This ensures better code quality and error detection. function processInput($input) {
    echo $input;
}
processInput(1);
function processInput(int $input) {
    echo $input;
}
processInput(1);
Support for anonymous class PHP 5.6 does not support anonymous classes, a feature introduced in PHP 7. This feature allows for the creation of classes without a name, useful for one-off implementations. PHP 8.x fully supports anonymous classes. class Hello {
    public $property = 'Hello';
    public function sayHello() {
        return $this->property;
    }
};
$object = new Hello();
echo $object->sayHello();
$object = new class {
    public $property = 'Hello';
    public function sayHello() {
        return $this->property;
    }
};
echo $object->sayHello();
String contains PHP 5.6 lacks a built-in contains function for strings. In PHP 8.x and later, the str_contains function was introduced to check if a string contains a given substring. This modern function simplifies string containment checks and improves code readability. if (strpos('abc', 'a') == false) echo "contains"; if (str_contains('abc', 'a')) echo "contains";
Null coalescing operator The null coalescing operator (??) was introduced in PHP 7.x. It provides a concise way to check if a value is set and not null, returning a default value if it is not. PHP 5.6 does not support this operator, requiring more verbose code for similar functionality. $value = isset($array['key']) ? $array['key'] : 'default'; $value = $array['key']) ?? 'default';
Undefined Variable In PHP 5.6, accessing an undefined variable triggers a notice but does not usually stop execution. PHP 8.x enforces stricter error handling and may generate more noticeable warnings or errors when accessing undefined variables. This change aims to encourage better error handling and more robust code. <?php
if($undefinedVariable) echo "do something";
<?php
if($undefinedVariable ?? false) echo "do something";
Undefined array key In PHP 5.6, accessing an undefined array key triggers a notice but does not generally halt execution. Starting from PHP 7.x, and more strictly in PHP 8.x and later, accessing an undefined array key generates a warning or error, encouraging better handling of array keys. <?php
$array = [];
if($array['key']) echo "do something"; else echo "do else";
<?php
$array = [];
if($array['key'] ?? false) echo "do something"; else echo "do else";
Undefined object In PHP 5.6, accessing properties on an undefined or non-existent stdClass object might trigger a notice but does not usually cause severe issues. In PHP 8.x, stricter error handling might lead to more noticeable errors when attempting to access properties on an undefined or improperly initialized stdClass object. <?php
if($obj->val) echo "value defined";
// If obj is not defined use null coalescing operator to avoid warnings
<?php
if($obj->val ?? false) echo "value defined";
Define constants In PHP 5.6, you could define constants using keys without quotes in some cases. In PHP 8.x, PHP enforces that constant names must be valid identifiers and must be defined with quotes around strings. <?php
define(NAME, "name");
<?php
define("NAME", "name");
Suppressing with @operator In PHP 5.6, the @ operator was commonly used to suppress errors, including notices and warnings. However, starting with PHP 7.x and continuing into PHP 8.x, many errors previously suppressed by @ are now treated more strictly. <?php
if(@constant("NAME")) echo "defined";
<?php
if(defined("NAME") echo "defined";
Usage of constructor with class name In PHP 5.6, using a method with the same name as the class for the constructor was standard practice. Starting from PHP 7.x, this style is deprecated and constructors should use the __construct method. PHP 8.x continues to enforce this change, making it necessary to update legacy code for compatibility. <?php
class Hello {
function Hello() {echo "hi";}
}
new Hello();
<?php
class Hello {
function __construct() {echo "hi";}
}
new Hello();
Too few arguments to function In PHP 5.6, calling a function or method with too few arguments might lead to a notice or warning, but the function might still execute with default values if they are provided. Starting with PHP 7.x and continuing in PHP 8.x, the handling of missing arguments is stricter, and functions with required parameters will result in a fatal error if not all arguments are provided. <?php
function hello($param){};
hello();
<?php
function hello($param){};
hello(1);
Import file within another file - Use path relative from doc_root We recommend importing files using an absolute path relative to the doc_root rather than using relative paths.   require(get_cfg_var('doc_root') . '/include/about.phph');
Constant already defined In PHP 5.6, redefining a constant that has already been defined does not raise an error but simply results in a notice. PHP 8.x enforces stricter error handling, and attempting to redefine an already defined constant will result in a fatal error. <?php
define('constant', 'value1');
define('constant', 'value2');
<?php
define('constant', 'value1');
if(!defined('constant'))    
define('constant', 'value2');
Creation of dynamic like user::$name is deprecated In PHP 5.6, dynamically creating properties on classes (e.g., user::$name) was possible but not recommended. Starting from PHP 7.x, this practice is deprecated, and PHP 8.x and later enforce stricter rules. Dynamically setting properties on classes that do not explicitly declare them is deprecated and will result in warnings or errors.
class User {
    // No properties defined here
}
$user = new User();
$user->name = "John Doe";  // This will trigger the deprecation warning in newer PHP versions
class User {
    public $name;
}
$user = new User();
$user->name = "John Doe";  // This is the recommended way and won't trigger any warnings
Function which is overridden must be compatible with the parent class function In PHP 5.6, when overriding a method in a subclass, there was limited enforcement of compatibility with the parent class method signature. PHP 7.x and later versions, including PHP 8.x, enforce stricter rules for method overriding to ensure that the overridden method is compatible with the parent class method in terms of parameters and return types.
class ParentClass {
    public function exampleMethod($a, $b=0){
    }
}
class ChildClass extends ParentClass {
    public function exampleMethod($a, $b){
    }
}
$child = new ChildClass();
echo $child->exampleMethod(10,11);
class ParentClass {
    public function exampleMethod($a, $b=0){
    }
}
class ChildClass extends ParentClass {
    public function exampleMethod($a, $b=0){
    }
}
$child = new ChildClass();
echo $child->exampleMethod(10,11);
in_array - add proper null checks wherever applicable. PHP 8 throws fatal error if array is null In PHP 5.6, using in_array with a null array would trigger a warning, but the function might still behave unexpectedly. Starting with PHP 8.x, in_array will throw a fatal error if the array is null. To ensure compatibility with PHP 8.x, you need to add proper null checks before using in_array.
$fruits = null;
if (in_array("banana", $fruits)) {
    echo "Banana is in the array!";
} else {
    echo "Banana is not in the array.";
}
$fruits = null;
if (isset($fruits) && in_array("banana", $fruits)) {
    echo "Banana is in the array!";
} else {
    echo "Banana is not in the array.";
}
count - check for null else fatal error is thrown. Similar issues exists with multiple functions like strlen. In PHP 5.6, using functions like count and strlen with null values would generally result in a warning, but the function might still work in some cases. PHP 8.x and later versions, including PHP 8.x, enforce stricter checks and will throw a fatal error if these functions are called with null values. To ensure compatibility with PHP 8.x, you need to add proper null checks before using these functions. $fruits = null;
echo count($fruits)
$fruits = null;
echo count($fruits ?? [])
The use of ${var} inside strings is deprecated in PHP in favor of {$var}. In PHP 5.6, you could use ${var} syntax inside double-quoted strings to interpolate variables. This syntax is deprecated in favor of {$var} starting from PHP 7.x. PHP 8.x continues to enforce this change, and using ${var} will result in a deprecated warning. $name = "John";
echo "Hello, ${name}!";  // Deprecated
$name = "John";
echo "Hello, {$name}!";  // Recommended