PHP 7.3 Function create_function() deprecated from 5.6 update [duplicate] - php

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 2 years ago.
I have recently upgraded a site im working on from PHP Version 5.6 to 7.3
The problem I am having is with a deprecated function in my wordpress functions.php
add_filter('max_srcset_image_width', create_function('', 'return 1;'));
How would i rewrite the above code for 7.3
I have already tried:
add_filter('max_srcset_image_width', function('', 'return 1;'));
Thanks

You can use functions directly like closures.
add_filter('max_srcset_image_width', function() { return 1; });
Update:
Since PHP 7.4 you can use shorthand arrow functions and write it shorter.
add_filter('max_srcset_image_width', fn() => 1);

Related

create_function() error in badwordsfitler_npm [duplicate]

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Upgrading from create_function to closures
(1 answer)
Closed 5 months ago.
Hello, everyone.
I am using badwords-filter npm and it is using create_function().
But as you know that function is removed from php 8.
So it is getting error now and I can't sure how to fix it.
If you are familiar with it, please help me.
private function flattenArray($array)
{
$objTmp = (object)['aFlat' => []];
array_walk_recursive($array, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objTmp);
return $objTmp->aFlat;
}
On the above code, the create_function() is getting error.

PHP Function create_function() is deprecated , the code cant work in newest PHP? [duplicate]

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 1 year ago.
I have a function which im not entirely sure how to convert it to get working in newest php
$eventSponsor = array_map(create_function('$o', 'return $o["id"];'), $event->sponsors);
which method should i use in newest php version ?
yeah i was searching and i found this method called anonymous function so the code be like
$awe function ($o) {
return $o["id"];
};
$eventSponsor = array_map($awe,$event->sponsors); ```

The each() function is deprecated. This message will be suppressed on further calls PHP 7.2 [duplicate]

This question already has answers here:
How can I update code that uses the deprecated each() function?
(12 answers)
Closed 4 years ago.
I have the below each() line in a PHP file on a server where I recently upgraded the PHP version from 5 to 7.
while(list($file, $info) = each($this->images))
The error below is thrown by the web server after the restart.
The each() function is deprecated. This message will be suppressed on further calls
What will be the correct way of re-writing the above line of code in PHP 7.2?
Thank you.
You should be able to swap out your each for a foreach in the most part.
<?php
foreach($this->images as $file => $info) {
// ...
}

Deprecated create_function() call [duplicate]

This question already has answers here:
PHP 7.2 Function create_function() is deprecated
(6 answers)
Closed 3 years ago.
I just updated my server to the latest version of php 7.2 and now I have some depreciation warnings. What should I do?
This function has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
Here is my code:
if(!array_key_exists('callable', $this->translation_plural)) {
$this->translation_plural['callable'] = create_function('$n', $this->translation_plural['function']);
}
The documentation recommends using anonymous functions. Given that $this->translation_plural['function'] looks like it is a string, you should consider a rewrite.
If you want to get rid of the warning, you can use the following:
$this->translation_plural['callable'] = function($n) { return eval($this->translation_plural['function']); };
This doesn't help your code at all, you are still using eval() which is bad practise. The documentation warns against using it.
The only difference is, create_function() used it internally, now it is very explicit.

Unexpected T_FUNCTION for function on PHP 5.2 [duplicate]

This question already has an answer here:
Making anonymous functions from PHP 5.3 work with PHP 5.2
(1 answer)
Closed 8 years ago.
I'm using the following function:
function uniqueACF(array $array) {
$flatten = array();
array_walk_recursive($array, function($value) use(&$flatten) {
$flatten[] = $value;
});
$flatten = array_unique($flatten);
return $flatten;
}
This works fine in PHP 5.3, but for PHP 5.2 it will give a 'unexpected T_Funcion for the array_walk_recursive line. It probably has to do with the function($value) or use(&$flatten).
But I'm not sure how I should make this work on PHP 5.2. How should I rewrite this for PHP 5.2?
Anonymous functions are available from PHP 5.3. That's why it doesn't work in PHP 5.2.
You should really think, why you move to PHP 5.2 - even PHP 5.3 reached already end of life and PHP 5.6 will be released soon.

Categories