Deprecated: Function create_function() is deprecated

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Uisge Beatha

Uisge Beatha

24/05/2019 17:05:04
Quote Anchor link
Heb mijn PHP versie naar 7.2 moeten upgraden om het cms te late werken en nu krijg ik in een van de programma's een fout melding dat create_function niet meer wordt ondersteund. De regel die de mist in gaat is:
$compare = create_function('$a, $b', $code);

Iemand die wil aangeven hoe deze regel herschreven kan worden zodat 7.2 het ook akkoord vindt?

Alvast dank!
 
PHP hulp

PHP hulp

29/03/2024 09:21:34
 
- Ariën  -
Beheerder

- Ariën -

24/05/2019 17:30:46
Quote Anchor link
Dit al gelezen?
Quote:
If you are using PHP 5.3.0 or newer a native [url=https://www.php.net/manual/en/functions.anonymous.php[/url] anonymous function should be used instead.
 
Rob Doemaarwat

Rob Doemaarwat

24/05/2019 18:39:01
Quote Anchor link
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
$compare = function($a,$b){
  return eval($code);
}

Maar beter is het om de $code gewoon uit te schrijven (als dat kan; als het het niets "magisch" is wat weer uit een database, of andere hoek van je programma komt). Dus stel dat:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
$code = '$a <=> $b';
//dan wordt:
$compare = function($a,$b){
  return $a <=> $b;
}
 
Uisge Beatha

Uisge Beatha

24/05/2019 18:49:36
Quote Anchor link
Dank je Rob. Ik zie o.b.v. jouw reactie dat ik de beter direct de hele functie had kunnen plaatsen omdat het tot een andere error leidt en $code in het vervolg van de functie nog nodig is.
NB. de bouwer van deze module is niet-bereikbaar en ik 'spreek' te weinig PHP om dit op te lossen. Vandaar de vraag.

Quote:
public static function ArraySort($array, $arguments = array(), $keys = true) {
// source: http://nl2.php.net/manual/en/function.uasort.php#42723
// comparing function code
$code = "\$result=0; ";

// foreach sorting argument (array key)
foreach ($arguments as $argument)
{
if (!empty($argument))
{
// order field
$field = substr($argument, 2, strlen($argument));

// sort type ("s" -> string, "n" -> numeric)
$type = $argument[0];

// sort order ("+" -> "ASC", "-" -> "DESC")
$order = $argument[1];

// add "if" statement, which checks if this argument should be used
$code .= "if (!Is_Numeric(\$result) || \$result == 0) ";

// if "numeric" sort type
if (strtolower($type) == "n")
{
$code .= $order == "-" ? "\$result = (\$a->{$field} > \$b->{$field} ? -1 : (\$a->{$field} < \$b->{$field} ? 1 : 0));" : "\$result = (\$a->{$field} > \$b->{$field} ? 1 : (\$a->{$field} < \$b->{$field} ? -1 : 0)); ";
}
else
{
// if "string" sort type
$code .= $order == "-" ? "\$result = strcoll(\$a->{$field}, \$b->{$field}) * -1;" : "\$result = strcoll(\$a->{$field}, \$b->{$field}); ";
}
}
}
// return result
$code .= "return \$result;";

// create comparing function
$compare = create_function('$a, $b', $code);

// sort array and preserve keys
uasort($array, $compare);

// return array
return $array;
}
 
Rob Doemaarwat

Rob Doemaarwat

24/05/2019 19:52:30
Quote Anchor link
Nou, vooruit dan, rondje van de zaak:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

public static function ArraySort($array, $arguments = array(), $keys = true) {
  uasort($array,function($a,$b) use ($arguments){
    $result = 0;

    foreach ($arguments as $argument) if (!empty($argument)){

      if (!Is_Numeric($result) || $result == 0){
        // order field
        $field = substr($argument, 2, strlen($argument));
        // sort type ("s" -> string, "n" -> numeric)
        $type = $argument[0];
        // sort order ("+" -> "ASC", "-" -> "DESC")
        $order = $argument[1];

        if (strtolower($type) == "n"){ // if "numeric" sort type
          $result = ($order == "-")
            ? (
$a->$field > $b->$field ? -1 : ($a->$field < $b->$field ? 1 : 0))
            : (
$a->$field > $b->$field ? 1 : ($a->$field < $b->$field ? -1 : 0));
        }

        else // if "string" sort type
        {
          $result = ($order == "-")
            ?
strcoll($a->$field, $b->$field) * -1
            : strcoll($a->$field, $b->$field);
        }
      }
    }


    return $result;
  });


  // return array
  return $array;
}


?>

Garantie tot de deur. Ik kreeg in ieder geval geen parse errors (ship it!), maar ik had geen data om mee te testen.
 
Uisge Beatha

Uisge Beatha

25/05/2019 07:13:19
Quote Anchor link
- Ariën - op 24/05/2019 17:30:46:

Dit al gelezen?
Quote:
If you are using PHP 5.3.0 or newer a native [url=https://www.php.net/manual/en/functions.anonymous.php[/url] anonymous function should be used instead.
Die had ik gelezen, maar lukt me nog niet deze te vertalen naar een oplossing van de foutmelding.
 
Rob Doemaarwat

Rob Doemaarwat

25/05/2019 13:06:08
Quote Anchor link
Dat kan korter:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

public static function ArraySort($array, $arguments = array(), $keys = true) {
  uasort($array,function($a,$b) use ($arguments){
    $result = 0;

    foreach ($arguments as $argument) if (!empty($argument)){
      $field = substr($argument, 2, strlen($argument));

      if($result = (strtolower($argument[0]) == "n")
        ?
$a->field <=> $b->$field // if "numeric" sort type
        : strcoll($a->$field, $b->$field) // if "string" sort
      ){
        if ($argument[1] == "-") // sort order ("+" -> "ASC", "-" -> "DESC")
          $result *= -1;

        break;
      }
    }


    return $result;
  });


  // return array
  return $array;
}


?>
 
Uisge Beatha

Uisge Beatha

26/05/2019 03:07:43
Quote Anchor link
Rob Doemaarwat op 25/05/2019 13:06:08:
Dat kan korter:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

public static function ArraySort($array, $arguments = array(), $keys = true) {
  uasort($array,function($a,$b) use ($arguments){
    $result = 0;

    foreach ($arguments as $argument) if (!empty($argument)){
      $field = substr($argument, 2, strlen($argument));

      if($result = (strtolower($argument[0]) == "n")
        ?
$a->field <=> $b->$field // if "numeric" sort type
        : strcoll($a->$field, $b->$field) // if "string" sort
      ){
        if ($argument[1] == "-") // sort order ("+" -> "ASC", "-" -> "DESC")
          $result *= -1;

        break;
      }
    }


    return $result;
  });


  // return array
  return $array;
}


?>

DDank je Rob. Deze leidt alleen tot een foutmelding
Quote:
Notice: Undefined property: stdClass::$field
en dat gebeurt op deze regel:
Quote:
? $a->field <=> $b->$field // if "numeric" sort type


Toevoeging op 26/05/2019 03:09:35:

Rob Doemaarwat op 24/05/2019 19:52:30:
Nou, vooruit dan, rondje van de zaak:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

public static function ArraySort($array, $arguments = array(), $keys = true) {
  uasort($array,function($a,$b) use ($arguments){
    $result = 0;

    foreach ($arguments as $argument) if (!empty($argument)){

      if (!Is_Numeric($result) || $result == 0){
        // order field
        $field = substr($argument, 2, strlen($argument));
        // sort type ("s" -> string, "n" -> numeric)
        $type = $argument[0];
        // sort order ("+" -> "ASC", "-" -> "DESC")
        $order = $argument[1];

        if (strtolower($type) == "n"){ // if "numeric" sort type
          $result = ($order == "-")
            ? (
$a->$field > $b->$field ? -1 : ($a->$field < $b->$field ? 1 : 0))
            : (
$a->$field > $b->$field ? 1 : ($a->$field < $b->$field ? -1 : 0));
        }

        else // if "string" sort type
        {
          $result = ($order == "-")
            ?
strcoll($a->$field, $b->$field) * -1
            : strcoll($a->$field, $b->$field);
        }
      }
    }


    return $result;
  });


  // return array
  return $array;
}


?>

Garantie tot de deur. Ik kreeg in ieder geval geen parse errors (ship it!), maar ik had geen data om mee te testen.

Een rondje van de zaak doet het altijd goed ;) Dank je! Deze doet het!
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.