PHP 8.1.28 Released!

forward_static_call

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

forward_static_callStatik yöntem çağrısı yapar

Açıklama

forward_static_call(callable $yöntem, mixed ...$bağımsız_değişkenler): mixed

Adı yöntem bağımsız değişkeninde belirtilen kullanıcı tanımlı işlevi veya yöntemi belirtilen bağımsız değişkenlerle çağırır. Bu işlev bir yöntem bağlamından çağrılmalı ve bir sınıfın dışında kullanılmamalıdır. İşlev Statik İçselleştirim kullanır.

Bağımsız Değişkenler

yöntem

Çağrılacak işlev veya yöntemin adı. Bu bağımsız değişken, sınıfın adını ve yöntemini içeren bir dizi veya işlev adına sahip bir dizge olabilir.

bağımsız_değişkenler

Bir veya daha fazla bağımsız değişken.

Dönen Değerler

Hata durumunda false, aksi takdirde işlevin dönüş değeri.

Örnekler

Örnek 1 - forward_static_call() örneği

<?php

class A
{
const
NAME = 'A';
public static function
test() {
$args = func_get_args();
echo static::
NAME, " ".join(',', $args)." \n";
}
}

class
B extends A
{
const
NAME = 'B';

public static function
test() {
echo
self::NAME, "\n";
forward_static_call(array('A', 'test'), 'more', 'args');
forward_static_call( 'test', 'other', 'args');
}
}

B::test('foo');

function
test() {
$args = func_get_args();
echo
"C ".join(',', $args)." \n";
}

?>

Yukarıdaki örneğin çıktısı:

B
B more,args
C other,args

Ayrıca Bakınız

  • forward_static_call_array() - Bağımsız değişkenleri dizi olarak iletilen statik yöntemi çağırır
  • call_user_func_array() - Bağımsız değişkenlerin bir dizi olarak verildiği geriçağırım işlevini çağırır
  • call_user_func() - İlk bağımsız değişkende belirtilen kullanıcı işlevini çağırır
  • is_callable() - Bir değişkenin değerinin bir işlev olarak çağrılabilirliğini doğrular

add a note

User Contributed Notes 2 notes

up
4
arthur dot techarts at gmail dot com
12 years ago
Example to understand this function and difference with call_user_func:

<?php
class Beer {
const
NAME = 'Beer!';
public static function
printed(){
echo
'static Beer:NAME = '. static::NAME . PHP_EOL;
}
}

class
Ale extends Beer {
const
NAME = 'Ale!';
public static function
printed(){
forward_static_call(array('parent','printed'));
call_user_func(array('parent','printed'));

forward_static_call(array('Beer','printed'));
call_user_func(array('Beer','printed'));
}
}

Ale::printed();
echo
'</pre>';
?>
up
1
jhibbard at gmail dot com
11 years ago
Example usage via calls outside of the class and within an object:

<?php
/**
* @author Jonathon Hibbard
*/
class foo {
# used to verify we're actually setting something..
private static $value = '';

/**
* Simple setter for the static method setValue...
*/
public static function set($method_identifier, $value_to_pass = '') {
# make sure we have the right method format...
# another semi-useful example is like this (useful for REST-like requests...): str_replace(" ", "", ucwords(str_replace("_", " ", $method_identifier)));
$static_method = 'set' . ucfirst(strtolower(trim($method_identifier)));

if(
method_exists(__CLASS__, $static_method)) {
//Note: this will not work and will throw PHP Parse error: syntax error, unexpected '::'
//__CLASS__::$static_method($value_to_pass);

foo::$static_method($value_to_pass);

echo
"\tCalling forward_static_call with pure string and value param:\n";
forward_static_call(__CLASS__ . "::" . $static_method, $value_to_pass);

echo
"\tCalling forward_static_call with class, method array and value param:\n";
forward_static_call(array(__CLASS__, $static_method), $value_to_pass);
}
}

/**
* Set self::$value to something?
*/
public static function setValue($value_recieved = '') {
echo
"\t\tsetValue called with param of " . var_export($value_recieved, true) . "!\n";

echo
"\t\tSetting Private 'value'...\n";

self::$value = $value_recieved;

echo
"\t\tChecking the Private 'value':\n";
if(!empty(
self::$value)) {
echo
"\t\t\tPrivate 'value' was set to '" . self::$value . "' as expected!\n";
} else {
echo
"\t\t\tPrivate 'value' was not set!\n";
}

# Reset...
self::$value = '';
}

/**
* Create an object and test calling the static method from within this realm...
*/
public function __construct() {
echo
"\tCalling from within constructor..\n";
foo::set('value','Something else from within the instance!');
}
}

echo
"\n============ Calling by static method first ============\n";
foo::set('value','Something from outside of the foo class!');

echo
"\n============ Calling by static method without a value next ============\n";
foo::set('value');

echo
"\n============ Calling by createing an instance next ============\n";
new
foo();
?>
To Top