call_user_func的使用

<?php
    function demo01($a)
    {
        echo $a;
    }

    call_user_func("demo01", "hello world");

 输出

 

1.配合命令行参数使用

<?php
//把第一个参数做为回调参数使用
$i = getopt("i:");
$i = $i['i'];
$arr = array(
    1=>"add",
    2=>"update"
);

function add()
{
    echo "执行新增操作";
}

function update()
{
    echo "执行更新操作";
}

call_user_func($arr[$i]);

 2.在类中使用

<?php

class test
{
    private $arr = array(
        1=>"add",
        2=>"update"
    );

    public function run ()
    {
        $i = getopt("i:");
        $i = $i['i'];

        call_user_func(array($this, $this->arr[$i]));
    }

    public function add()
    {
        echo "新增";
    }

    public function update()
    {
        echo "更新";
    }
}

$obj = new test();
$obj->run();

 结果:

posted @ 2020-07-08 11:49  子夜的流星  阅读(378)  评论(0)    收藏  举报