Funksie forward_static_call_array

Die funksie forward_static_call_array laat jou toe om 'n statiese metode van 'n klas op te roep deur parameters as 'n array deur te gee. Die eerste parameter aanvaar 'n callable (klas- en metode-naam in die formaat van 'n array of string), die tweede - 'n array van argumente om aan die metode oor te dra.

Sintaksis

phpforward_static_call_array(callable $function, array $parameters);

Voorbeeld

Laat ons 'n statiese metode van 'n klas oproep en parameters deurgee:

php<?php
  class Calculator {
    public static function add($a, $b) {
      return $a + $b;
    }
  }
  
  $res = forward_static_call_array(['Calculator', 'add'], [5, 3]);
  echo $res;
?>

Resultaat van die kode-uitvoering:

php8

Voorbeeld

Gebruik van 'n string-oproep in plaas van 'n array:

php<?php
  class StringHelper {
    public static function concat($str1, $str2) {
      return $str1 . $str2;
    }
  }
  
  $res = forward_static_call_array('StringHelper::concat', ['Hello', 'World']);
  echo $res;
?>

Resultaat van die kode-uitvoering:

php'HelloWorld'

Sien ook