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();
?>
forward_static_call
(PHP 5 >= 5.3.0)
forward_static_call — Llamar a un método estático
Descripción
Llama a una función o método definido por el usuario, dado por el parámetro
function, con los siguientes argumentos. Esta función debe ser llamda dentro
del contexto de un método, no se puede usar fuera de una clase.
Usa el Enlace estático
en tiempo de ejecución.
Parámetros
-
function -
La función o método a ser llamado. Este parámetro puede ser una matriz, con el nombre de la clase y del método, o una cadena, con el nombre una función.
-
parameter -
Cero o más parámetros a ser pasados a la función.
Valores devueltos
Devuelve el resultado de la función, o FALSE en caso de error.
Ejemplos
Ejemplo #1 Ejemplo de forward_static_call()
<?php
class A
{
const NOMBRE = 'A';
public static function prueba() {
$args = func_get_args();
echo static::NOMBRE, " ".join(',', $args)." \n";
}
}
class B extends A
{
const NOMBRE = 'B';
public static function prueba() {
echo self::NOMBRE, "\n";
forward_static_call(array('A', 'prueba'), 'más', 'args');
forward_static_call( 'prueba', 'otro', 'args');
}
}
B::prueba('foo');
function prueba() {
$args = func_get_args();
echo "C ".join(',', $args)." \n";
}
?>
El resultado del ejemplo sería:
B B más,args C otro,args
Ver también
- forward_static_call_array() - Llamar a un método estático y pasar los argumentos como matriz
- call_user_func_array() - Llamar a una llamada de retorno un array de parámetros
- call_user_func() - Llamar a una llamada de retorno dada por el primer parámetro
- is_callable() - Verificar que los contenidos de una variable puedan ser llamados como una función
- información acerca de tipos de llamada de retorno
jhibbard at gmail dot com ¶
11 months ago
arthur dot techarts at gmail dot com ¶
1 year 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>';
?>
