dongshun5963
2013-04-19 09:20
浏览 57
已采纳

类的PHP调用方法,其中类名在其他类的字段中

I have a CodeIgniter php setup and enhanced it with RESTful capabilities.

I have the following structure

base.php (this is the base controller class

class Base {
    private $model
    public function __construct($model = false) {
        $this->model = $model . '_model';
        $this->load->model($this->model);
    ...
}

And then a controller which specifies a model

class Products extends Base {

    public function __construct() {
        parent::__construct('product');     
    }
}

The problem is as follows: in base.php I have functions for HTTP methods (get, post, put, delete), but I am not able to call a static method from the model like so:

public function get() {
    return $this->model::loadData();
}

If I assign $this->model to a local variable in get() it works, but it looks ugly to me.

So my question is: how can I call a static method of a class A given the class name in a member of class B without assigning it to a new local variable in methods of class B?

P.S.: I know CodeIgniter does not look like this, but the structure of it is irrelevant to my problem.

图片转代码服务由CSDN问答提供 功能建议

我有一个CodeIgniter php设置并使用RESTful功能对其进行了增强。

我有以下结构

base.php(这是基本控制器类

  class Base {
 private $ model 
  public function __construct($ model = false){
 $ this-> model = $ model.'_model'; 
 $ this-> load-> model($ this-> model); 
。  .. 
} 
   
 
 

然后是指定模型的控制器

 类产品扩展基数{\  n 
公共函数__construct(){
 parent :: __ construct('product'); 
} 
} 
   
 
 

问题如下: 在base.php中我有HTTP方法的函数(get,post,put,delete),但是我无法从模型中调用静态方法,如下所示:

   public function get(){
 return $ this-> model :: loadData(); 
} 
   
 
 

如果我指定 $ this- >模型 get()它可以工作,但它看起来很难看。

所以我的问题是:如何在给定类名的情况下调用类A的静态方法 在B类成员中没有将它分配给B类方法中的新局部变量?

PS:我知道CodeIgniter看起来不像这样,但它的结构与它无关 我的问题。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题