我是一名朝九晚六的Java程序员,但在业余时间,我对PHP的研究很少。我想知道你们对使用这个类有什么想法,以及我可能有什么安全方面的考虑
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();我在考虑使用这个包含或扩展另一个类。
http://localhost/proyect/object.php?update=1
include_once("class.Action.php");
function update($id){
//the function
}为了记录,我不想使用这个框架,它的项目太小:P
发布于 2012-11-05 12:11:54
首先,您应该使用php5,它具有可见性、关键字和其他一些东西:
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 );
}
}您应该始终传入$_GET IMO,因此您的实例化现在看起来如下所示:
$action = new Action($_GET);
$action->callFunction();现在,就您试图在这里实现的目标而言,它是不清楚的。如果你试图从本质上构建一个路由类,我认为这是相当丑陋和容易出错的。
在你的评论中,你不想使用一个框架,因为这个项目很简单/很小,我强烈建议你试试Silex或Slim微框架,而不是从头开始构建。
例如,使用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(); https://stackoverflow.com/questions/13225725
复制相似问题