views:

139

answers:

2

Hi all,

I'm new in this site. I want to ask about PHP programming. How do we can handle deprecated function in PHP. I want to redirect it to my new function. As we know, ereg function has been deprecated in PHP 5.3.0 and recommended to preg_match (posix to PCRE). But, when we wrote a lot of code with ereg function, do we have to change it manually? I want a solution like this.

function ereg($pattern, $string, &$array) { return preg_match('#'.$pattern.'#', $string, $array); }

The main problem is not the ereg function, but solution of handling deprecated function. I've been searching in Google. Someone suggest to use override_function (using APD extension). But, this extension is hard to find (I need precompiled extension build for windows). Anyone can help me? I'm sorry for my bad English. I hope you can understand.

+4  A: 

The reason they tell you it is deprecated, and they don't remove it completely, is to give you time to update your code.

If you don't want to update your code, you can always just not upgrade your install of PHP. Or you can wait until a release of PHP is out were ereg() is removed completely, and use your above solution.

Other possible solutions include doing a search/replace for all ereg calls, and replacing it my_ereg, which could be the function you defined above.

Also:

if(!function_exists("ereg")){ .... }

Define the function inside of the if statement that checks if the function already exists. This will make the transition smoother.

But all in all, the purpose of deprecation is to give developers time to update their code and stop using all of the deprecated functions before they remove it completely from the code base.

I believe some call it 'Maintenance'.

Chacha102
Yeah...I know it. But updating million lines of code is very tiring. I've been tried the code you given above. But it didn't work. It may mean '**deprecated function is still available but it's inaccessible**'. So, it'll never <pre><code>if (!function_exists("ereg"))</code></pre> condition.
iroel
@iroel Welcome to the world of software development. Do you really have ***millions*** of calls to `ereg` in your code base? I doubt it, and your IDE's find/replace should make the update a little easier.
deceze
@iroel Generally deprecated functions will spawn an error message if you use them, but still work. The find/replace method will work well though..
Chacha102
@iroel Just remove the deprecated functions. You don't have to do it all at once, but in the year(s)+ until the next major PHP release you should have plenty of time.
CaseySoftware
The main problem is not replacing the code, but how to handle deprecated function. Since PHP 5.3.0, there are functions deprecated. Better solution is welcome than editting code manully. Maybe in the future there will be a function that can not be used again and I know you understand that. Do we have to always replace them manually for the next?Replacing code manually is the last choice.
iroel
@iroel Yes. When a function is deprecated you have to replace it manually. Always. (Especially if the `!function_exists()` or other techniques don't work.)
Chacha102
@iroel You always have the option to simply not upgrade your version of PHP. That is what a lot of really large internal corporate applications do. They don't have the manpower to migrate everything to the next version, so they don't upgrade.
Chacha102
@iroel Chacha mentioned several alternatives. The best way is to conditionally define your own version of the deprecated function. There's a reason why functions are deprecated though, usually because there's some problem with them. You will always have to handle deprecated functions on a case by case basis. Sometimes that means supplying your own implementation of a function, other times it means replacing every instance of it. Sometimes a find/replace replacement is all you need, sometimes you have to refactor code. That's just the way it is.
deceze
@deceze So it means I should use abstraction function for a compability like this?<br/><pre><code>define('PHP5_3', (version_compare(phpversion(), '5.3.0', '<') ? FALSE : TRUE));function regex($pattern, $string, }</code></string>May be it the best solution since PHP didn't have support for handling deprecated function. Thanks for advice :)
iroel
A: 

You could always use the function_exists function.

if(!function_exists('ereg'))
{
    function ereg($pattern, $string, &$array) 
    { 
        return preg_match('#'.$pattern.'#', $string, $array); 
    }
}

Using this method would allow it to work in all version as if it is deprecated but still able to be used it will use the function but once it has been removed from php it will be able to use your user defined function.

ozatomic
Thanks for the answer, but I tried it and it didn't work. Here is my code for testing:<pre>if (!function_exists('ereg')) { function ereg($pattern, $string, }}$email = '[email protected]';$match = ereg('[\w\.-_]+@[\w\.-_]+\.([a-z]{2,3})', $email) ? 'correct' : 'wrong';$match2 = preg_match('#[\w\.-_]+@[\w\.-_]+\.([a-z]{2,3})#i', $email) ? 'correct' : 'wrong';echo "Using ereg: $match<br>\n";echo "Using preg_match: $match2<br>\n";</pre>
iroel