php中通过url调用类的特定功能
hy,我在类中有两个函数,但我想通过点击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(
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