根据父控制器中的变量调用函数

发布于 2024-10-01 03:02:09 字数 1234 浏览 0 评论 0原文

我正在编写一个微型 MVC 框架,我希望能够根据给定 URL 中的操作运行一个函数。我目前这样做:

function action($id, $function){

${$id}->{$function}();

}

其中 $id 是要加载的模型/视图/控制器,$function 是操作。这显然有效,但在 MainController 中无效。我必须将其放入启动脚本中,这将受到限制,因为我希望人们能够从视图内部运行操作,以便他们可以根据他们提供的功能执行不同的操作。

Fatal error: Call to a member function testFunction() on a non-object in /home/cherwell/public_html/scott/develop/simple/system/controllers/MainController.php on line 20

有人知道解决方法,以便我可以从主控制器调用子控制器的函数吗?

干杯!

编辑: 我通过执行以下操作使操作起作用:

if($action != false){

${$id}->{$action}();

} else {

include("system/views/".$id.".php");
}

$id 和 $action 始终被定义,因此它工作正常。我现在遇到的唯一问题是任何函数都必须具有所有全局变量(它是一个很长的列表):

global $id; 
global ${$id};
global $site;
global $start;
global $action;
global $param;
global $helper;
global $model;

如果它们需要其中任何一个(如果您需要从主控制器/帮助器/模型调用方法,它们就会这样做)。 。

我不知道为什么变量没有被传递到函数中 我也声明了相应 ID 的类...

编辑 2: 例子。

function testFunction(){
global $helper;
$helper->test();

$this->render("home");

}

有效,但是当我删除 全局$helper; 它失败了。即使我在启动、子级和父级的构造中有大量全局变量,它仍然会失败。每个函数也会发生这种情况(需要全局变量)。有人可以帮忙吗?

I'm writing a micro MVC framework, and I want to be able to run a function based on the action in the URL given. I currently do:

function action($id, $function){

${$id}->{$function}();

}

Where the $id is the model/view/controller to load, and the $function is the action. This apparently works, but not in the MainController. I'd have to put it in the startup script which would be limiting as I want people to be able to run action from inside the view so that they can perform different actions based on what they give the function.

Fatal error: Call to a member function testFunction() on a non-object in /home/cherwell/public_html/scott/develop/simple/system/controllers/MainController.php on line 20

Anyone know a way around it so that I can call a function from the child controller from the MainController?

Cheers!

EDIT:
I have made the action work by doing:

if($action != false){

${$id}->{$action}();

} else {

include("system/views/".$id.".php");
}

$id and $action are always defined, so it works fine. The only problem I have now is that any functions must have all of the globals (its a long list):

global $id; 
global ${$id};
global $site;
global $start;
global $action;
global $param;
global $helper;
global $model;

if they need any of these (which, they do if you need to call methods from the maincontroller/helper/model)...

I don't know why variables aren't being passed down into the functions. I have declared the class for the respective ID too...

EDIT 2:
Example.

function testFunction(){
global $helper;
$helper->test();

$this->render("home");

}

Works, but when I remove the
global $helper;
it fails. Even if I have a load of globals in the startup, construct of child and parent, it still fails. This also happens for every function (needing globals). Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

动次打次papapa 2024-10-08 03:02:09

您是否尝试过使用专门的函数来防止此类编码?

function action($id, $function, $params = array())
{
    if(!is_object($id))
    {
        $id = new $id; //Or just use globalization or something to bring it in scope.
    }

    call_user_func_array(array($id,$function),$params);
}

have you tried use a function dedicated to prevent this sort of coding?

function action($id, $function, $params = array())
{
    if(!is_object($id))
    {
        $id = new $id; //Or just use globalization or something to bring it in scope.
    }

    call_user_func_array(array($id,$function),$params);
}
烟柳画桥 2024-10-08 03:02:09

在这种情况下 ${$id} 为 null,因为它尚未设置。
您是否正在尝试执行以下操作?

function action($id, $function){

   $controller = new $id();
   $controller->{$function}();

}

In this case ${$id} is null because it haven't been set.
Are you trying to do something like the following?

function action($id, $function){

   $controller = new $id();
   $controller->{$function}();

}
浅语花开 2024-10-08 03:02:09

怎么样:

$app = new myApp();
function action ($action)
{
    global $app;
    if (!method_exists($app,$action)) return;
    $app->$action();
}

How about:

$app = new myApp();
function action ($action)
{
    global $app;
    if (!method_exists($app,$action)) return;
    $app->$action();
}
━╋う一瞬間旳綻放 2024-10-08 03:02:09

我最终在主控制器中创建了一个 __init() 函数,该函数在加载站点时被调用。效果很好。现在模型/助手/视图和控制器可以完美工作。

不过还是谢谢你的帮助! :)

I ended up making an __init() function in the main controller that was called upon on loading of the site. Works nicely. Now models/helpers/view and controller work perfectly.

Thanks for the help though! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文