这是我的网址http://localhost/mymvc/index.php?url=controller/method 我想通过$ _GET超级全局变量捕获'方法'并获取Controller方法的访问类。 我想让所有方法动态化
这是我的索引文件代码。
<?php
include 'inc/header.php';
include_once 'system/libs/MController.php';
include_once 'system/libs/Main.php';
$url = $_GET['url'];
$url = rtrim($url, '/');
$url = explode("/", $url);
include 'app/controllers/'.$url[0].'.php';
$ctlr = new $url[0]();
$ctlr->$url[1](); //---->(1) here is problem. I can't access my method this way
include 'inc/footer.php';
?>
这是我的Controller.php文件代码
<?php
/**
* controller
*/
class Controller extends MController{
public function __construct(){
// parent::__construct();
}
public function method(){
echo "Mohammad Arman from method";
}
}
?>
但是当我显示此输出时。在此错误和通知下方显示
**
1)注意:数组到字符串的转换 第15行的C:\Users\USER\Documents\wamp\www\mymvc\index.php
2)注意:未定义的属性:Controller :: $Array在 第15行的C:\Users\USER\Documents\wamp\www\mymvc\index.php
3)致命错误:未捕获错误:函数名称必须为字符串 C:\Users\USER\Documents\wamp\www\mymvc\index.php:15堆栈跟踪:#0 {main}在C:\Users\USER\Documents\wamp\www\mymvc\index.php中抛出 第15行**
分析解答
您需要确保正确定义了用作函数名称的名称,
$ctlr->$url[1]();
可以解释为$ctlr->$url
,然后解释为[1]
,但您真正想要的是...
$ctlr->{$url[1]}();