我創建了一個php函數來返回或保存一些jsons,並且類看起來像這樣。從字符串調用php函數名稱
<?php
class calendarModel {
// global variables
var $user;
var $action;
var $connect;
// init class
function __construct($action = "getEvents") {
$this->user = 1;
$this->action = $action;
$dbhost = "localhost";
$dbport = "5432";
$dbname = "fixevents";
$dbuser = "postgres";
$dbpass = "123";
$this->connect = pg_connect("host=" . $dbhost . " port=" . $dbport . " dbname=" . $dbname . " user=" . $dbuser . " password=" . $dbpass);
$this->executeAction();
}
// action router
function executeAction() {
if($this->action == "getEvents")
$this->getEvents();
else if($this->action == "moveEvent")
$this->moveEvent();
else if($this->action == "insertEvent")
$this->insertEvent();
else if($this->action == "updateEvent")
$this->updateEvent();
else if($this->action == "getCalendars")
$this->getCalendars();
else if($this->action == "toggleCalendar")
$this->toggleCalendar();
else if($this->action == "deleteCalendar")
$this->deleteCalendar();
else if($this->action == "insertCalendar")
$this->insertCalendar();
}
// getEvents
function getEvents() {
//...
}
// moveEvent
function moveEvent() {
//...
}
// insertEvent
function insertEvent() {
//...
}
// updateEvent
function updateEvent() {
//...
}
// toggleCalendar
function toggleCalendar() {
//...
}
// deleteCalendar
function deleteCalendar() {
//...
}
// insertCalendar
function insertCalendar() {
//...
}
}
// call class
if(isset($_GET['action']))
$instance = new calendarModel($_GET['action']);
else
$instance = new calendarModel();
?>
什麼,我想知道是,我可以以某種方式實現從字符串名稱contruct的行動,而不是作出這樣大,如果如果函數調用/其他executeAction
。丹尼爾,先謝謝你!
'call_user_func( )' – zerkms
@zerkms'$ this'雖然會使事情變得複雜。 – kapa
@kapa:'call_user_func(array($ this,'methodname'))' – zerkms