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!

5
  • @zerkms $this will complicate things though. Commented Feb 12, 2014 at 22:58
  • @kapa: call_user_func(array($this, 'methodname')) Commented Feb 12, 2014 at 22:58
  • @zerkms Hm, haven't used PHP for some time. I thought you can only pass strings in the array. Thanks for clearing it up. Commented Feb 12, 2014 at 23:01
  • i tried call_user_func($action); and $action(); but they dont work Commented Feb 12, 2014 at 23:07
  • @Pacuraru Daniel: you probably read function documentation before using it. Commented Feb 12, 2014 at 23:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much this works as intended :D i wish i could give you guys both correct answer :)
0

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

5 Comments

thank you very much for this, this works perfect, i dont even need to make the execute Action function anymore, i just called it like this inside the contruct
This answer doesn't deserve to be checked, sorry
$action is a class property, you need $this->
actually i just used it exactly like this and it works for me, maybe it is because i am using it into the contructor
ops, it was a lack of attention of my part :/ Barmar is correct :)
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();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.