Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Struts中使用类似Java操作的PHP类?_Php - Fatal编程技术网

在Struts中使用类似Java操作的PHP类?

在Struts中使用类似Java操作的PHP类?,php,Php,我是一名9到6岁的Java程序员,但在业余时间我很少学习PHP。 我只是想知道你们对使用这个类有什么看法,我可能有什么安全考虑 class Action{ var $func; var $param; function Action(){ $url_keys = array_keys($_GET); $this->func = $url_keys[0]; $this->param = $_GET[$this

我是一名9到6岁的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

首先,您应该使用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 );
    }
}
您应该始终传入
$\u GET
IMO,因此您的实例化现在如下所示:

$action = new Action($_GET);
$action->callFunction();
现在,就你在这里试图实现的目标而言,还不清楚。如果您试图从本质上构建一个路由类,我认为这是非常丑陋的,而且很容易出错

关于你不想使用框架的评论,因为这个项目很简单/很小,我敦促你签出或使用微框架,而不是从头开始构建

例如,对于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(); 

尝试使用用于PHP的Slim框架。这对小项目很有好处。谢谢,我要看看这个框架!!我不知道这件事你这里缺少安全资料,我编辑了你的code@Bang例如我什么也没缺。它不在原始代码中,我放入代码块的唯一原因是显示从php4到php5的函数和属性定义的差异。事实上,我不建议你使用他所有的东西,即使是你的编辑:-)b/c在他提到的关于安全考虑的问题中,所以我认为你应该在你的回答中添加这些,我甚至没有读过那部分。。。被php4的肮脏分心。感谢你的帮助!我还认为它可能也容易出错。。。我最好检查一下你提到的那些框架。。。苗条看起来真的是我想要的!!