0

我的课:

class mvc {
    public function home () {
        return 'index';
    }
}

目的:

include "./sys/controller/".$Current_page_Controller_name.".php";

        // making object of the controller class.
            $controller = new $Current_page_Controller_name;

        // Cheks if model is called, m stands for "model"
            if (isset($_GET['m'])) {

            }

            $method_name = $mvc->$Current_page_Controller_name.'()';

    // Cheks if default model exists, if not gives an error.
        if (method_exists($controller, $method_name)) {
            echo "+";
        } else {
            echo "-";
            //die("Lappas sledzis saplisis, gaidiet drizuma... (2)");
        }

我不断收到此错误

Notice: Undefined property: mvc::$home in /home/unusuallv/domains/.../public_html/sys/config.php on line 51

问题就在 $method_name = $mvc->$Current_page_Controller_name.'()'; 我知道,但我想不通:// tnx 很多!

4

3 回答 3

0

您正在调用该函数,这就是错误。你不想叫它,但要知道它的名字。为此,您不需要$method_name = $mvc->$Current_page_Controller_name.'()';而是$method_name=Current_page_Controller_name;然后这样调用它:$mvc->$method_name().

当 PHP 正在处理您的脚本时,它认为$mvc->$Current_page_Controller_name.'()'您正在访问变量$mvc,然后附加到它 string '()'

考虑这些例子:

//somewhere file
class MVC{
public $someVar="QWERTY";
public function abc(){/*...*/}
public function parseResponse(){/*...*/}
};
///.....
$mvc=new MVC;
$abc="someVar";
$x=$mvc->$abc."()";
echo $x;//gives you "QWERTY()"

$b=method_exists($mvc,$abc);//false
$b=method_exists($mvc,'abc');//true
//call function
$abc="parseResponse";
$mvc->$abc();//calls MVC::parseResponse
于 2013-01-05T23:12:11.330 回答
0
$method_name = $mvc->$Current_page_Controller_name.'()';

应该是这样的:

$method_name = $mvc->$Current_page_Controller_name();
于 2013-01-05T23:25:28.657 回答
0

在以下表达式中:


$method_name = $mvc->$Current_page_Controller_name.'()';

->运算符的优先级高于字符串连接运算符 ( .)。这意味着 PHP$mvc->$Current_page_Controller_name首先计算表达式,因此它会尝试访问 mvc 类的 home 属性(它不存在)。

改用这个:


$method_name = $mvc->$Current_page_Controller_name();
于 2013-01-05T23:28:29.340 回答