可以在 php 运行时将函数分配给类变量吗?

发布于 2024-12-21 17:57:52 字数 397 浏览 4 评论 0原文

是否可以在运行时将要执行的函数分配给类变量?一种像 C

这样的“函数指针”:(这不起作用,因为 sum 超出了 A 的范围,但这就是我的意思)

 class A {
    public $function_name;
    public functon run($arg1,$arg2){
        $function_name($arg1,$arg2);
    }
}  

function sum($a,$b){
    echo $a+$b;
}

$a=new A();
$a->function_name='sum';

$a->run();     

[编辑] 我知道有“call_user_func”,但据我了解,它需要在范围内包含该函数或使用公共类方法

it is possible to assign to a class variable a function at runtime to be executed? a kind of "function pointer" like C

something like this: (this won't work because sum is out of the scope of A, but this is the pattern i mean)

 class A {
    public $function_name;
    public functon run($arg1,$arg2){
        $function_name($arg1,$arg2);
    }
}  

function sum($a,$b){
    echo $a+$b;
}

$a=new A();
$a->function_name='sum';

$a->run();     

[edit]
i know there is "call_user_func" but it need as i understand to have the function in the scope or use a public class method

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦中的蝴蝶 2024-12-28 17:57:52

如果您使用 PHP > 5.3.0,则可以使用 匿名函数

$sum = function($a, $b) {
    return $a+$b;
}

$a->function_name = $sum;

You could use an anonymous function if you use PHP >5.3.0:

$sum = function($a, $b) {
    return $a+$b;
}

$a->function_name = $sum;
深海夜未眠 2024-12-28 17:57:52

使用 call_user_func_array

<?php
class A {

    public $function_name;

    public function run($arg1,$arg2){

        return call_user_func_array( $this->function_name, array($arg1, $arg2 ) );

    }
}  

function sum($a,$b){
    return $a+$b;
}

$a=new A();
$a->function_name= 'sum';

var_dump( $a->run(1,1) ); //2

?>

Using call_user_func_array:

<?php
class A {

    public $function_name;

    public function run($arg1,$arg2){

        return call_user_func_array( $this->function_name, array($arg1, $arg2 ) );

    }
}  

function sum($a,$b){
    return $a+$b;
}

$a=new A();
$a->function_name= 'sum';

var_dump( $a->run(1,1) ); //2

?>
但可醉心 2024-12-28 17:57:52

无论范围如何,它都有效。您只需使用 call_user_func。我还修复了您的示例中的一些拼写错误。

<?php
    class A {
        public $function_name;
        public function run($arg1, $arg2) {
        call_user_func($this->function_name, $arg1, $arg2);
        }
    }  

    function sum($a, $b){
        echo $a + $b;
    }

    $a = new A();
    $a->function_name = 'sum';

    $a->run(2, 3);
?>

实例

It works regardless of scope. You just gotta call it using call_user_func. I also fixed a couple of typos in your example.

<?php
    class A {
        public $function_name;
        public function run($arg1, $arg2) {
        call_user_func($this->function_name, $arg1, $arg2);
        }
    }  

    function sum($a, $b){
        echo $a + $b;
    }

    $a = new A();
    $a->function_name = 'sum';

    $a->run(2, 3);
?>

Live example

早乙女 2024-12-28 17:57:52

另一种方式是使用可变变量(适用于对象方法)

public static function sum($arg1, $arg2)
{
  ..
}

public function run($arg1, $arg2)
{
  $func = $this->function_name;
  $func( $arg1, $arg2);         <-- procedural call
  self::$func($arg1, $arg2);    <-- static method call
}

Another way is to make use variable variables (applicable to object method)

public static function sum($arg1, $arg2)
{
  ..
}

public function run($arg1, $arg2)
{
  $func = $this->function_name;
  $func( $arg1, $arg2);         <-- procedural call
  self::$func($arg1, $arg2);    <-- static method call
}
七月上 2024-12-28 17:57:52

使用 Callback 伪类型的任何变体。
call_user_funccall_user_func_array

该手册给出了上述内容的很好的使用示例。

如果您想,另请参阅新的 php 5.4 Closure::bindTO 方法能够轻松地使用其中的 $this 关键字。

Use any variation of the Callback pseudo type.
Use it with call_user_func or call_user_func_array

The manual gives great examples of usage for the above.

Also see the new php 5.4 Closure::bindTO method if you want to be able to easily use the $this keyword in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文