Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中通过url调用类的特定功能_Php_Json_Function_Methods - Fatal编程技术网

php中通过url调用类的特定功能

php中通过url调用类的特定功能,php,json,function,methods,Php,Json,Function,Methods,hy,我在类中有两个函数,但我想通过点击url中的函数名来调用类的特定函数 class RedeemAPI { private $db; // Constructor - open DB connection function __construct() { $this->db = new mysqli('localhost', 'root', '', 'promos'); $this->db->autocommit(

hy,我在类中有两个函数,但我想通过点击url中的函数名来调用类的特定函数

class RedeemAPI {

    private $db;

    // Constructor - open DB connection
    function __construct() {
        $this->db = new mysqli('localhost', 'root', '', 'promos');
        $this->db->autocommit(FALSE);
    }

    // Destructor - close DB connection
    function __destruct() {
        $this->db->close();
    }

    // Main method to redeem a code
    function redeem() {
        echo 'someone is here before';
    }

 function users ()  {
        $array = array();
        $array[0]= "user 1";
        $array[1]= "user 2";
        $array[2]= "user 3";


        sendResponse(200, json_encode($array), 'application/json');
        return true;
    }
}
$api = new RedeemAPI;
$api->redeem();
$api->users();

通过这段代码,两个函数同时调用,我在url中点击url test/apicall.php/users/

write something.php?function=users

...
$function = $_GET['function'];
$api = new RedeemAPI;
$api->$function();
...
但是这种调用函数的方式是非常不安全的。请小心使用,希望您知道自己在做什么;)


如果您需要这样的URL:Test/ApCalal.Pp/UsRs/,请考虑使用.Access解析“用户”作为函数变量。比如。php?function=users¶m1=value1¶m2=value2