PHP函数不执行

时间:2014-04-07 12:12:18

标签: php

我试图从地址栏调用类函数,如下所示:

http://localhost:82/spam_fetcher.php?rm=index

我的脚本是:

  class Spam_fetcher{
    public function __construct()
    {
        if (isset($_GET['rm']) && method_exists('Spam_fetcher', $_GET['rm'])) {
            $view = new Spam_fetcher();
            $view->$_GET['rm']();
        }
        else
        {
            echo "No such a function";
        }
    }

    public function index()
    {
        echo 'something';
    }
}

但索引功能不会执行。你们认为我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

class Spam_fetcher{
    public function __construct()
    {
        if (isset($_GET['rm']) && method_exists($this, $_GET['rm'])) {
           $this->$_GET['rm']();
        }
        else
        {
            echo "No such a function";
        }
    }

    public function index()
    {
        echo 'something';
    }
}

$view = new Spam_fetcher();