【发布时间】:2017-10-25 23:11:23
【问题描述】:
我在 Stackoverflow 上搜索了很多并寻找了我的问题的答案,但我没有找到答案,在其他地方也没有。
无论如何,我多次尝试使用 CodeIgniter 的驱动程序,但我经常因为 __call 方法而退出。让我用一个例子来解释。
假设我有一个名为 App 的驱动程序(当然它扩展了 CI_Driver_Library)
class App extends CI_Driver_Library
{
public $CI;
public $valid_drivers;
public function __constructor()
{
$this->CI &= get_instance();
$this->valid_drivers = ['users']; // An example.
}
}
现在我有了像这样的 users 驱动程序。
class App_users extends CI_Driver
{
private $CI;
public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->model('users_model');
}
public function __call($method, $params = [])
{
if (method_exists($this, $method)
{
return call_user_func_array(array($this, $method), $params);
}
elseif (method_exists($this->CI->users_model, $method))
{
return call_user_func_array(array($this->CI->users_model, $method), $params);
}
else
{
throw new BadMethodCallException("No such method: {$method}");
}
}
}
我想在这个 App_users __constructor 中加载 Users_model(可能但 parent::$CI 不可访问,但我管理)。
我面临的问题是当使用 __call 方法来调用此驱动程序的方法 OR 模型的方法时。不幸的是,这种神奇的方法对我没有用,也从来没有用过。如果可能的话,我该怎么做?如果没有,是否有任何解决方法?
提前致谢。
【问题讨论】:
-
请向我们展示您对
__call的实现。 -
@DFriend 我编辑了代码并添加了我的实现(请不要介意拼写错误)
标签: php codeigniter libraries drivers magic-methods