略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: call_user_method

2024-04-23

call_user_method

(PHP 4, PHP 5)

call_user_method对特定对象调用用户方法

Warning

This function was DEPRECATED in PHP 4.1.0, and REMOVED in PHP 7.0.0.

Alternatives to this function include:

说明

call_user_method ( string $method_name , object &$obj [, mixed $parameter [, mixed $... ]] ) : mixed

参数

method_name

要调用的方法名称。

obj

要调用的 method_name 所在的对象 object

parameter ...

可选参数

范例

Example #1 代替 call_user_method()

<?php
call_user_func
(array($obj$method_name), $parameter /* , ... */);
?>

参见

add a note add a note

User Contributed Notes 6 notes

up
1
bmafzallmahmud at gmail dot com
1 year ago
<?php
call_user_method
()
This function was DEPRECATED in PHP 4.1.0, and REMOVED in PHP 7.0.0.
Alternatives to this function include : call_user_func()

example :01

class Habib {
    static public function
test() {
        print
"Hello Hasina!<br>";
    }

}

call_user_func('Habib::test');
call_user_func(array('Habib', 'test'));
//this class no __NAMESPACE__  dut we use and find the result
call_user_func(__NAMESPACE__ .'\Habib::test');
call_user_func(array(__NAMESPACE__ .'\Habib', 'test'));

$classname = "Habib";
call_user_func(array($classname, 'test'));
call_user_func($classname .'::test');
$myobject = new Habib();
call_user_func(array($myobject, 'test'));
call_user_func($myobject .'::test'); //Recoverable fatal error : Object of class Foo could not be converted to string

output:
Hello Hasina!
Hello Hasina!
Hello Hasina!
Hello Hasina!
Hello Hasina!
Hello Hasina!
Hello Hasina!
Recoverable fatal error : Object of class Foo could not be converted to string

example
:02
--------------------------------------------------------------------------
namespace
Foobar;

class
Habib {
    static public function
test() {
        print
"Hello Hasina!<br>";
    }

}
call_user_func(__NAMESPACE__ .'\Habib::test');
call_user_func(array(__NAMESPACE__ .'\Habib', 'test'));
$myobject = new Habib();
call_user_func(array($myobject, 'test'));

output:
Hello Hasina!
Hello Hasina!
Hello Hasina!
up
0
j dot h at h-elektro dot de
12 years ago
It does not work to use Pointers as Arguments:

<?php
class abc{
   function
func(&$argument)  {
      
$argument="It works";
   }
}

$obj=new abc;
$argument_to_be_changed="No it doesnt";
call_user_method("func", $obj, $argument_to_be_changed);

echo
"Result".$argument_to_be_changed;
?>

The result is: "No it doesnt".

Regards
der Jan
up
0
paulo at emd dot com dot br
18 years ago
This function is very similar to this:

<?php
$method
="Print";
$object->$method($param1,$param2);
?>

Note the extra $ after the ->
up
-1
ravichandran_11 at yahoo dot co dot in
11 years ago
<?php
class abc{
   function
func($argument)  {
      
$argument="It works";
   }
}
$obj=new abc;
$argument_to_be_changed="No it doesn't work";
call_user_method("func", $obj, &$argument_to_be_changed);
echo
"Result : ".$argument_to_be_changed;
?>

This code is working. But will through some warning message which you can hide by configuring php.ini
up
-2
jmcastagnetto at php dot net
18 years ago
You can pass a variable number of parameters to a function, use a definition like:

function mymethod ($v1, $v2, $v3="", $v4="")

and then you can pass 2, 3 or 4 parameters. This is explained in the "Functions" section of the manual.

See also the PHP4 functions: func_num_args(), func_get_arg(), and func_get_args(), and examples therein
up
-8
Chris
10 years ago
From what i've observed, call_user_func() does everything this funciton does and a little more.  I made a pretty good example call_user_func()'s usage with object instances and figured it might be useful here:

<?php

   
/**
   
        This is a demonstration of 2 neat features of PHP
       
        *    passing array arguments in as a big array, and using += to assign defaults to missing values
                This would allow for function calls that more closely mimick thoes made in javascript using JSON, with enough work, it could be almost identical using associative arrays
        *    function callbacks within a class to global instances of other classes
                This allows you pass a function callback to an object early on, and hold off its execution until later in the program (say during page outputing after everything has been setup)
   
    **/

   
class Runner {
   
        public
$id;
       
        public function
__construct($id) {
           
$this->id = $id;
            echo
"constructing " . __CLASS__ . " with id of $id<br />\n";
        }
       
        public function
run($distance = null, $measurement = 'km') {
            if (
$distance) {
                echo
'I ran ' . $distance . ' ' . $measurement . '.';
            } else {
                echo
'I ran.';
            }
            echo
"({$this->id})<br />\n";
        }
    }
   
    class
Speaker {
   
        public
$id;
   
        public function
__construct($id = 0) {
           
$this->id = $id;
            echo
"constructing " . __CLASS__ . " with id of $id<br />\n";
        }
       
        public function
speak($statement = 'hello world') {
            echo
$statement . "({$this->id})<br />\n";
        }
    }

    class
Test {
        protected
$runCallback = null;
        protected
$speakCallback = null;
        protected
$statement;
        protected
$distance;
       
        public function
__construct(array $params = array()) {
            echo
"constructing " . __CLASS__ . "<br />\n";
           
$params += array('speakCallback' => array('Speaker', 'speak'), 'runCallback' => array('Runner', 'run'), 'statement' => 'Hello from ' . __CLASS__ . ' class!', 'distance' => 10);
            foreach(
$params as $k => $v) {
               
$this->$k = $v;
            }
        }
       
        public function
getInstance() {
            return new
self(current(func_get_args()));
        }
       
        public function
callRunner() {
            if (
is_callable($this->runCallback))
                return
call_user_func($this->runCallback, $this->distance);
            else
                throw new
Exception("runCallback is not callable\n" . var_export($this->runCallback, true) . "\n");
        }
        public function
callSpeaker() {
            if (
is_callable($this->speakCallback))
                return
call_user_func($this->speakCallback, $this->statement);
            else
                throw new
Exception("speakCallback is not callable\n" . var_export($this->speakCallback, true) . "\n");
        }
    }
   
   
$r = new Runner(1);
   
$s = new Speaker(2);
   
   
// Note that we're using $s instead of 'Speaker'
   
call_user_func(array($s, 'speak'), 'Hello from global!');
   
   
// try out from global with call_user_func_array() to pass args as an array
   
call_user_func_array(array($r, 'run'), array(5, 'mi'));
   
   
   
$Test = new Test(array('runCallback' => array($r, 'run'), 'speakCallback' => array($s, 'speak')));
   
$Test->callRunner();
   
$Test->callSpeaker();
   
   
   
$Test = call_user_func(array('Test', 'getInstance'), array('runCallback' => array($r, 'run'), 'distance' => 15));
   
// should work as expected
   
$Test->callRunner();
   
// should throw an error for trying to use this during a static call to Speaker::speak() because of the default
   
$Test->callSpeaker();
   
   
?>

Hope that's helpful.

官方地址:https://www.php.net/manual/en/function.call-user-method.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3