0

i created a php function to return or save some jsons, and the class looks like this.

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

What i was wondering is, can i somehow call the action in the contruct from the string name instead make that big if / else if function called executeAction. Thank you in advance, Daniel!

Pacuraru Daniel
  • 1,207
  • 9
  • 30
  • 56

3 Answers3

3

If you use an expression where the function name will be used, the value of the expression will be used as the function name:

function executeAction() {
    $this->{$this->action}();
}

Since you're getting the action from user input, make sure you validate it. Otherwise, someone could send input that makes you execute an arbitrary method.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Barmar is almost correct: $this->{$action}();

0

Use something like that:

function __construct($action = "getEvents")
{
    ...
    $this->$action();
}

As $action is defined by the user, you might want to check whether $action is an existing function in your class:

if (array_key_exists($action, get_class_methods($this))) {
    $this->$action();
}
Bernhard Frick
  • 119
  • 1
  • 11