How to handle deprecated function in PHP - php

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.

Related

PHP openssl free key deprecated

In php the function
openssl_free_key
is deprecated and not includde in php 8. Can somebody give an alternative for it?
This function is now deprecated as it doesn't have an effect anymore.
https://www.php.net/manual/en/function.openssl-free-key.php
PHP 8 deprecates openssl_free_key (actually openssl_pkey_free which it aliases) and automatically destroys the key instance when it goes out of scope.
https://www.php.net/manual/en/function.openssl-pkey-free.php#125655
So really you replace it with nothing, as the resource is now cleaned up properly the same as anything else.
If you want to feel like you're going that extra mile you can call unset() on it, but that's not a thing I would consider necessary.
In case you need to maintain compatibility with older php version, you can do something like this:
// $privateKey = openssl_pkey_new([ ... ]);
if (PHP_VERSION_ID < 80000) {
openssl_free_key($privateKey);
}

Alternative for deprecated PHP function: kd_mfi_get_featured_image_id

Hello I have used this wp function kd_mfi_get_featured_image_id that is deprecated. But in error log actually I have a big amount of rows due to this function and I think that affect on performances too.
It is also used by my theme that is one of the most used (Avada).
So I wanna ask you an alternative for this one.
The kd_mfi_get_featured_image_id function has been deprecated since
Avada 5.2.0. Please use fusion_get_featured_image_id instead.
Taken from here

Add removed/deprecated functions to PHP5

Is it possible to add a removed/deprecated function to PHP5? Like session_is_registered, ereg, etc.
[update] solved for session_is_registered:
<?php
function session_is_registered($name) {
return isset($_SESSION[$name]);
}
thanks.
Of course you can do it by modifying and recompiling the PHP source code, however the first question you have to answer is Do I really need to this or I might be better to go for my IDE's find-and-replace function?
If there is a real need for this -- for whatever reason, maybe you can redefine those functions. I haven't test it yet, as I agree with others that functions and features get removed or deprecated for a good and mostly important reasons, so I'm not sure if it does work in a situation that the function is removed or depreciated, but you can try to redefine them either using runkit_function_redefine or
override_function.
In that case you have to simulate the functionality again -- probably with their good-to-go replacements, so again think twice before start doing that.

Remove function alias in PHP

Is there a way to remove function alias in PHP?
I can rename my function but it would be nice to use name "fetch".
Problem:
I just tested the following code and it appears to work for me, but perhaps it is because I don't have the mysqli library installed. I would test it because it might be more contextual than your IDE will have you believe. It seems to be a method for mysqli, but it might not be a global function.
<?php
function fetch(){
echo 'Hello world!';
}
fetch();
No.
(Short of recompiling the PHP binary)
This is more of a function of the IDE than the actual language... Some IDEs may give you that ability... I don't even know if recompiling the PHP binary (as Alan Storm suggested) would help since sometimes the stuff is hardcoded into the IDE / use the PHP docs online
For completeness sake: Normally, no, this can not be done. However: this can be done using a PECL extension called "runkit".
Runkit is described as "For all those things you probably shouldn't have been doing anyway", and allows you to basically tear out the innards of PHP from within PHP itself. Replacing built-in functions, undefining constants, unloading classes - suddenly everything is possible. And you should really question what you are doing if you ever feel you need it - odds are what you are doing violates some principles that are there for very good reasons, you just don't know them yet. I've never found a situation where using Runkit was a genuinely Good Idea.
Oh, in order to remove built-in functions you'll specifically need to enable this capability in your php.ini
(have fun!)

My php can't find 'normalizer_normalize()' function, why?

How come I can't use normalizer function?
I am using it exactly as I should, ie: $var = normalizer_normalize($str);
I get php error: no such function!
Here is the code:
$headline= mysql_real_escape_string($_POST['headline']);
$ad_id_string=normalizer_normalize($headline);
Thanks
From the docs, normalizer_normalize was included in PHP as of version 5.3.0. Prior to that, it was available as a PECL extension. To build it as part of PHP, you'll need to install internationalization support.
If you need to provide your own equivalent to the Normalizer class, strtr() would be a good option. The only downside is that you will need to provide your own character strings to translate to / from.

Categories