Various PHP 5.3 deprecated code fixes

Posted by Matt Thommes on October 7, 2009 | Post type: Gain

With the release of PHP 5.3 have come many necessary code changes due to deprecated warnings.

This is intended to be a running tally of changes that should be made to specific code syntax, in order to avoid PHP warnings in your applications. This is, by no means, a complete list, but as I hear about them, I will continue to add them to this post.

ereg_replace

Code sample

$message = ereg_replace("\r", '', $message);

What the script is trying to do

Perform a pattern match, then replace.

Warning that you'll get

"Function ereg_replace is deprecated"

What needs to be done to fix it

Since we're using a simplified regular expression in this example (\r), we can safely switch to str_replace() without having to change the parameters.

This:

$message = ereg_replace("\r", '', $message);

... should become:

$message = str_replace("\r", '', $message);


Code sample

$output = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" target=\"_blank\">\\0</a>", $output);

What the script is trying to do

Perform a regular expression pattern match, then replace. In this case, we're replacing all URL's with HTML hyperlinks.

Warning that you'll get

"Function ereg_replace is deprecated"

What needs to be done to fix it

ereg_replace needs to become preg_replace, but you can't just swap the name of the function. You have to re-write the first parameter regular expression pattern match. (I am not posting that here because I don't know what it is, for the example above.)

Assigning by reference

Code sample

$a = &new $func($param1, $param2);

What the script is trying to do

Assign the return value of new by reference.

Warning that you'll get

"Assigning the return value of new by reference is deprecated"

What needs to be done to fix it

This:

$a = &new $func($param1, $param2);

... should become:

unset($a);
$a = new $func($param1, $param2);

Code sample

$this->files['error'] = &new HTTP_Upload_File( ...

What the script is trying to do

Assign the return value of new by reference.

Warning that you'll get

"Assigning the return value of new by reference is deprecated"

What needs to be done to fix it

This:

$this->files['error'] = &new HTTP_Upload_File( ...

... should become:

$this->files['error'] = null;
$this->files['error'] = new HTTP_Upload_File( ...

session_unregister

Code sample

session_unregister('user');

What the script is trying to do

Unset a session variable.

Warning that you'll get

"Function session_unregister() is deprecated"

What needs to be done to fix it

This:

session_unregister('user');

... should become:

unset($_SESSION['user']);

About the author(s)

Matt Thommes is an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Comments

Note: Comments may be viewed by authors, but if you have a more specific question you'd like to ask them, please email matt.thommes@paininthetech.com.

# Danny Gunawan at 11/18/2009 10:06 am cst

Great article. So many people gives workarounds but no one create an article contain collection fixes like you do. Thanks Matt. Btw I am from Indonesia.

Quick Link to this comment: http://TTIP.me/c5420