从字符串中调用php函数名称

帕库拉鲁·丹尼尔(Pacuraru Daniel)

我创建了一个php函数来返回或保存一些json,该类看起来像这样。

<?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();
?>

我想知道的是,如果可以,我可以以某种方式从字符串名称中调用该构造函数中的操作,而不是使它变大executeAction提前谢谢你,丹尼尔!

用户3303864

Barmar几乎是正确的:$ this-> {$ action}();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章