开发者

How to handle deprecated function in PHP

开发者 https://www.devze.com 2023-01-08 06:51 出处:网络
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

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.


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'.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消