根据父控制器中的变量调用函数
我正在编写一个微型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过使用专门的函数来防止此类编码?
have you tried use a function dedicated to prevent this sort of coding?
在这种情况下 ${$id} 为 null,因为它尚未设置。
您是否正在尝试执行以下操作?
In this case ${$id} is null because it haven't been set.
Are you trying to do something like the following?
怎么样:
How about:
我最终在主控制器中创建了一个 __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! :)