note 60984 added to functions.variable-functions

From: Date: Sun, 22 Jan 2006 18:07:14 +0000
Subject: note 60984 added to functions.variable-functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-102243@lists.php.net to get a copy of this message
If you want to call a static function (PHP5) in a variable method: Make an array of two entries where the 0th entry is the name of the class to be invoked ('self' and 'parent' work as well) and the 1st entry is the name of the function. Basically, a 'callback' variable is either a string (the name of the function) or an array (0 => 'className', 1 => 'functionName'). Then, to call that function, you can use either call_user_func() or call_user_func_array(). Examples: <?php class A { protected $a; protected $c; function __construct() { $this->a = array('self', 'a'); $this->c = array('self', 'c'); } static function a($name, &$value) { echo $name,' => ',$value++,"\n"; } function b($name, &$value) { call_user_func_array($this->a, array($name, &$value)); } static function c($str) { echo $str,"\n"; } function d() { call_user_func_array($this->c, func_get_args()); } function e() { call_user_func($this->c, func_get_arg(0)); } } class B extends A { function __construct() { $this->a = array('parent', 'a'); $this->c = array('self', 'c'); } static function c() { print_r(func_get_args()); } function d() { call_user_func_array($this->c, func_get_args()); } function e() { call_user_func($this->c, func_get_args()); } } $a =& new A; $b =& new B; $i = 0; A::a('index', $i); $a->b('index', $i); $a->c('string'); $a->d('string'); $a->e('string'); # etc. ?> ---- Server IP: 64.71.164.2 Probable Submitter: 67.184.93.222 ---- Manual Page -- http://www.php.net/manual/en/functions.variable-functions.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60984 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60984&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60984&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60984&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60984&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60984&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60984&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60984&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60984&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102243) next »