2013-02-14 107 views
0

我在創造一個用魔術__call函數調用在WordPress API_Widgets類的問題。如果我簡單文件derp.php和類重命名爲API_Derp,然後它工作沒有問題。PHP:__call不能正常工作

下面的例子已被剝奪一切不重要這個問題的(因此,如果有比第三代碼塊中指定的特定致命錯誤其他任何錯誤,忽略它)。

請記住,我知道core.phpAPI類'__call作品,因爲重命名widgets.php或調用另一個類的作品很好。

core.php中:

class API { 

    function __call($method, $args) { 

     $rmethod = "API_{$method}"; 
     if (!class_exists($rmethod)) { 

      $lmethod = strtolower($method); 
      require_once("{$lmethod}.php"); 

     } 

     return new $rmethod($args); 

    } 

} 

widgets.php:

class API_Widgets { 

    private $widgets = array(); 

    function Add($widgets) { 

     if (is_array($widgets)) { 

      foreach ($widgets as $widget) { 

       if (!in_array($widget, $this->widgets)) 
        $this->widgets[] = $widget; 

      } 

     } else 
      $this->widgets[] = $widgets; 

    } 

} 

api.php:

$api = new API(); 
$widgets = $api->Widgets(); // Fatal error: Class 'API_Widgets' not found in /home4/goldencr/public_html/wp-content/plugins/minecraft-api/api/core.php on line 25 
//$widgets->Add('some_widget'); 
+0

我沒有當我運行它得到一個錯誤:http://codepad.viper-7.com/6pvBi9 – 2013-02-14 02:56:35

+1

你也看到'include' FILE_NOT_FOUND問題?你可以嘗試使用像'__DIR __。$ lmethod絕對路徑。 「PHP」'。 – Passerby 2013-02-14 03:05:16

+0

絕對路徑解決了我的問題。我在其他地方使用絕對路徑,我只是不知道它在這裏沒有被使用。作出回答,我會接受它作爲答案。 @過路人 – 2013-02-14 03:10:16

回答

1

從註釋擴展:

雖然在沒有暗示你問題,似乎你可能實際上不在包括widgets.php。嘗試使用絕對路徑來解決這個問題:

require_once(__DIR__.DIRECTORY_SEPARATOR.$lmethod.".php");