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);
}
Related
Is there any way to easily fix this issue or do I really need to rewrite all the legacy code?
PHP Fatal error: Call-time pass-by-reference has been removed in ... on line 30
This happens everywhere as variables are passed into functions as references throughout the code.
You should be denoting the call by reference in the function definition, not the actual call. Since PHP started showing the deprecation errors in version 5.3, I would say it would be a good idea to rewrite the code.
From the documentation:
There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);.
For example, instead of using:
// Wrong way!
myFunc(&$arg); # Deprecated pass-by-reference argument
function myFunc($arg) { }
Use:
// Right way!
myFunc($var); # pass-by-value argument
function myFunc(&$arg) { }
For anyone who, like me, reads this because they need to update a giant legacy project to 5.6: as the answers here point out, there is no quick fix: you really do need to find each occurrence of the problem manually, and fix it.
The most convenient way I found to find all problematic lines in a project (short of using a full-blown static code analyzer, which is very accurate but I don't know any that take you to the correct position in the editor right away) was using Visual Studio Code, which has a nice PHP linter built in, and its search feature which allows searching by Regex. (Of course, you can use any IDE/Code editor for this that does PHP linting and Regex searches.)
Using this regex:
^(?!.*function).*(\&\$)
it is possible to search project-wide for the occurrence of &$ only in lines that are not a function definition.
This still turns up a lot of false positives, but it does make the job easier.
VSCode's search results browser makes walking through and finding the offending lines super easy: you just click through each result, and look out for those that the linter underlines red. Those you need to fix.
PHP and references are somewhat unintuitive. If used appropriately references in the right places can provide large performance improvements or avoid very ugly workarounds and unusual code.
The following will produce an error:
function f(&$v){$v = true;}
f(&$v);
function f($v){$v = true;}
f(&$v);
None of these have to fail as they could follow the rules below but have no doubt been removed or disabled to prevent a lot of legacy confusion.
If they did work, both involve a redundant conversion to reference and the second also involves a redundant conversion back to a scoped contained variable.
The second one used to be possible allowing a reference to be passed to code that wasn't intended to work with references. This is extremely ugly for maintainability.
This will do nothing:
function f($v){$v = true;}
$r = &$v;
f($r);
More specifically, it turns the reference back into a normal variable as you have not asked for a reference.
This will work:
function f(&$v){$v = true;}
f($v);
This sees that you are passing a non-reference but want a reference so turns it into a reference.
What this means is that you can't pass a reference to a function where a reference is not explicitly asked for making it one of the few areas where PHP is strict on passing types or in this case more of a meta type.
If you need more dynamic behaviour this will work:
function f(&$v){$v = true;}
$v = array(false,false,false);
$r = &$v[1];
f($r);
Here it sees that you want a reference and already have a reference so leaves it alone. It may also chain the reference but I doubt this.
In the PHP documentation for session_unset() there is no hint that this function is deprecated so I think it's fine to use it. But then I read through the documentation about session_destroy() where I found this hint:
Note:
Only use session_unset() for older deprecated code that does not use $_SESSION.
I know that the session_unset() function is an equivalent for $_SESSION = array();. So what should I use now? Why is on one site a hint that this function is deprecated but on the other hand in the documentation about the function itselfs there is not deprecated note. What's the truth about this function now?
I don't know the exact reason, too, but it can be found here I guess: https://github.com/php/php-src/blob/master/ext/session/session.c
PHP handles variables in ZVAL pointers and I think they just wanted the _SESSION superglobal to be handled the same way as any other variable, and not with a "special" command session_unset().
Another benefit is better garbage collection handling I think.
Sometimes, "deprecated" does not mean its a bad function, but you should not use it because code might be removed in future packages simply because it is not neccessary.
If you want to close the session you must use session_destroy().
session_destroy();
If you want to clear the variables of the session you must use:
$_SESSION = array();
And if you want to clear only one variable of the session you must use:
unset($_SESSION['example']);
Is there any way to easily fix this issue or do I really need to rewrite all the legacy code?
PHP Fatal error: Call-time pass-by-reference has been removed in ... on line 30
This happens everywhere as variables are passed into functions as references throughout the code.
You should be denoting the call by reference in the function definition, not the actual call. Since PHP started showing the deprecation errors in version 5.3, I would say it would be a good idea to rewrite the code.
From the documentation:
There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);.
For example, instead of using:
// Wrong way!
myFunc(&$arg); # Deprecated pass-by-reference argument
function myFunc($arg) { }
Use:
// Right way!
myFunc($var); # pass-by-value argument
function myFunc(&$arg) { }
For anyone who, like me, reads this because they need to update a giant legacy project to 5.6: as the answers here point out, there is no quick fix: you really do need to find each occurrence of the problem manually, and fix it.
The most convenient way I found to find all problematic lines in a project (short of using a full-blown static code analyzer, which is very accurate but I don't know any that take you to the correct position in the editor right away) was using Visual Studio Code, which has a nice PHP linter built in, and its search feature which allows searching by Regex. (Of course, you can use any IDE/Code editor for this that does PHP linting and Regex searches.)
Using this regex:
^(?!.*function).*(\&\$)
it is possible to search project-wide for the occurrence of &$ only in lines that are not a function definition.
This still turns up a lot of false positives, but it does make the job easier.
VSCode's search results browser makes walking through and finding the offending lines super easy: you just click through each result, and look out for those that the linter underlines red. Those you need to fix.
PHP and references are somewhat unintuitive. If used appropriately references in the right places can provide large performance improvements or avoid very ugly workarounds and unusual code.
The following will produce an error:
function f(&$v){$v = true;}
f(&$v);
function f($v){$v = true;}
f(&$v);
None of these have to fail as they could follow the rules below but have no doubt been removed or disabled to prevent a lot of legacy confusion.
If they did work, both involve a redundant conversion to reference and the second also involves a redundant conversion back to a scoped contained variable.
The second one used to be possible allowing a reference to be passed to code that wasn't intended to work with references. This is extremely ugly for maintainability.
This will do nothing:
function f($v){$v = true;}
$r = &$v;
f($r);
More specifically, it turns the reference back into a normal variable as you have not asked for a reference.
This will work:
function f(&$v){$v = true;}
f($v);
This sees that you are passing a non-reference but want a reference so turns it into a reference.
What this means is that you can't pass a reference to a function where a reference is not explicitly asked for making it one of the few areas where PHP is strict on passing types or in this case more of a meta type.
If you need more dynamic behaviour this will work:
function f(&$v){$v = true;}
$v = array(false,false,false);
$r = &$v[1];
f($r);
Here it sees that you want a reference and already have a reference so leaves it alone. It may also chain the reference but I doubt this.
My site has a deprecated error at this line:
$obj =& new $class($table,$primkeyArr,$this);
it is because of &. It gives this error:
Deprecated: Assigning the return value of new by reference is deprecated in ....
It is a issue in php 5.3.
If I remove the & from this line, the error does away. But I don't know if it causes any problem if I put my site on a server with lower PHP version (5.2) or not.
Will removing the & work ok both in PHP 5.2 and in PHP 5.3?
In PHP 5, objects are handled in a reference-like manner by default. So removing the & probably won't change anything.
But as assigning by reference breaks old references, there might still be a difference.
It's not deprecated to return a reference but to not reflect that in your functions oder methods signature. There has to be an & before the name as well as when assigning the returned value.
public function &getValue()
{
return $this->value;
}
...
$myValue = &$obj->getValue();
The manual will tell you more.
http://php.net/manual/en/language.references.pass.php
It's a mere warning. You don't really need it, but the short answer is that it will only throw warnings. Depending on the level of error_handling in your php config will depend on if you see it on other 5.3 systems.
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.