Amit Dhamu

Software Engineer

Deprecations in PHP 7

Each version of PHP brings with it certain deprecations. See below for a list of popular ones up until PHP 7.2.


split

// Deprecated
split(',', $foo);
 
// Substitute
preg_split('/,/', $foo);

create_function

// Deprecated
uasort($list,
    create_function('$a, $b',
        'return $a->getName()>$b->getName();'
    )
);
 
// Substitute
uasort($list, function ($a, $b) {
    return $a->getName() > $b->getName();
});

each

// Deprecated
while(list($key, $val) = each($data)) {
 
}
 
// Substitute
foreach ($data as $key => $val) {
 
}

preg_replace with the /e modifier

// Deprecated
preg_replace('/href="(.*?)"/ie',
    "foo('$link', '\\1', '$eid', '$id')",
    $string
);
 
// Substitute
preg_replace_callback('/href="(.*?)"/i', function ($matches) use ($link, $eid, $id) {
    return foo($link, $matches[1], $eid, $id);
}, $string);

ereg

// Deprecated
ereg("^foobarbaz", $filename);
 
// Substitute
preg_match('/^foobarbaz/', $filename);

eregi

// Deprecated
eregi("^FooBarBaz", $filename);
 
// Substitute
preg_match('/^FooBarBaz/i', $filename);

ereg_replace

// Deprecated
ereg_replace(',,', ',', $foo);
 
// Substitute
preg_replace('/,,/', ',', $foo);

$HTTP_GLOBALS

Deprecated Substitute
$HTTP_RAW_POST_DATA php://input
$HTTP_SESSION_VARS $_SESSION
$HTTP_SERVER_VARS $_SERVER
$HTTP_POST_VARS $_POST
$HTTP_GET_VARS $_GET
$HTTP_ENV_VARS $_ENV
$HTTP_COOKIE_VARS $_COOKIE

track_errors INI directive

// Deprecated
ini_set('track_errors', 1);
echo $php_errormsg;
 
// Substitute
echo error_get_last()['message']

Indirect access to variables, properties and methods.

These will be evaluated strictly in left-to-right order since PHP 7.0. Use curly braces to remove ambiguity.

// Change code like this...
$data = $model->$field['function']();
 
// ...to this
$data = $model->{$field['function']}();

Using parse_str without result parameter

// Deprecated
$a = parse_str($q);
 
// Substitute
parse_str($q, $a);

session_is_registered

// Deprecated
if (!session_is_registered('userid')) {
 
}
 
// Substitute
if (!$_SESSION['userid']) {
 
}

__autoload

// Deprecated
function __autoload($classname) {
    require_once(strtolower($classname) . '.class.php');
}
 
// Substitute
spl_autoload_register(function ($classname) {
    require_once(strtolower($classname) . '.class.php');
});

PHP 4 Style Constructors

// Deprecated
class Foo
{
    public function Foo()
    {
        // constructor
    }
}
 
// Substitute
class Foo
{
    public function __construct()
    {
        // constructor
    }
}

 php7

Managing Oh My Zsh Using AntigenExtending Class Methods in PHP 7

Show Comments

Made with by Amit Dhamu.
© MMXX. All rights reserved.