Deprecated functions problems in PHP 5.3 series

Discussion for TimeTrex open source community developers.
Locked
paulocoghi
Posts: 1
Joined: Tue Mar 30, 2010 11:35 am

Deprecated functions problems in PHP 5.3 series

Post by paulocoghi »

Hi!

I'm installing TimeTrex Open Source here in Brazil. We use Apache 2.2 and PHP 5.3.1

I'm having problems during installation beacuse Profiler.class.php (line 193) uses split() function, that is deprecated.

Fixing the problem is easy*, but my difficulty is in making this correction throughout all the project. These errors will probably appear over time in other files in the project, not only in the installation process, so it is very difficult do corrections in a large group of files, without the risk of making mistakes or harm the project.

Thus, this task is simple for anyone who already knows the project, since it involves only small replacements.

PHP.net tip:
preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.
* In the article above, the author explains how to solve the problem in a simple and elegant way
http://devthought.com/tumble/2009/06/fi ... in-php-53/

Here is some examples:

To migrate split() :

Code: Select all

split('[()]', $string);
becomes

Code: Select all

preg_split('/[()]/', $string);

To migrate ereg() :

Code: Select all

ereg('\.([^\.]*$)', $this->file_src_name, $extension);
becomes

Code: Select all

preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);

Notice that I wrapped the pattern (\.([^\.]*$)) around / /, which are RegExp delimiters. If you find yourself escaping / too much (for an URL for example), you might want to use the # delimiter instead.

To migrate ereg_replace() :

Code: Select all

$this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body);
becomes

Code: Select all

$this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body);

Again, I just added delimiters to the pattern.
If you are using eregi functions (which are the case-insensitive version of ereg), you’ll notice there’re no equivalent pregi functions. This is because this functionality is handled by RegExp modifiers.

Basically, to make the pattern match characters in a case-insensitive way, append i after the delimiter:

Code: Select all

eregi('\.([^\.]*$)', $this->file_src_name, $extension);
becomes

Code: Select all

preg_match('/\.([^\.]*$)/i', $this->file_src_name, $extension);

If any member has a broader knowledge of the project and knows where these functions may appear, their aid will be of great value to the community and for the project, with just a few replacements. :wink:

Thank you!
shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: Deprecated functions problems in PHP 5.3 series

Post by shaunw »

TimeTrex v3.1.0-rc1 has just been released which should mostly run using PHP v5.3, since TimeTrex does rely on PEAR modules, there may still be some PEAR modules that have not yet been updated to support PHP v5.3, but I think 99% of TimeTrex should work now.
http://forums.timetrex.com/viewtopic.php?f=8&t=1776
Locked