Question

I'm 9 to 6 Java programer but in my spare time I have little proyects in PHP. Just wondering what do you guys think of using this class and what security considerations I might have

class Action{

    var $func;
    var $param;

    function Action(){

        $url_keys = array_keys($_GET);
        $this->func = $url_keys[0];
        $this->param = $_GET[$this->func];
    }

    function callFunction(){
        $f = $this->func;
        $f( $this->param );
    }
}

$ajax = new Action();
$ajax-> callFunction();

I was thinking to using this including or extending another class.

http://localhost/proyect/object.php?update=1

include_once("class.Action.php");


function update($id){
 //the function
}

For the record I don't want to use a framework this proyect its to small :P

Was it helpful?

Solution

Well first of all you should be working with php5 which has visibility keywords and some other things:

class Action {

    protected $func;
    protected $param;

    public function __construct($params = array()){

        $url_keys = array_keys($params);
        $this->func = $url_keys[0] . "Action"; // so use can not call function without surfix "Action" in this class
        $this->param = $params[$this->func];
    }

    public function callFunction(){
        $f = $this->func;
        return $f( $this->param );
    }
}

You should always pass in $_GET IMO so your instantiation now looks like this:

$action = new Action($_GET);
$action->callFunction();

Now as far as what youre trying to accomplish here it is unclear. If youre trying to essentially build a routing class i think this is pretty ugly and error prone as is.

In terms of you comment about not wanting to use a framework because the project is simple/small id urge you to check out Silex or Slim micro frameworks instead of building from scratch.

For example with Silex:

$app = new Silex\Application(); 

$app->get('/object/update/{id}', function($id) use($app) { 
    // do your update with $id
    // then return a response

    return 'Update Complete';
    // basically you return whatever response you want so normally youd return html. 
}); 

$app->run(); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top