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条回答 默认 最新

  • doufang7385 2013-04-19 10:34
    已采纳

    As you point out, assigning $this->model to a local variable like $model and then calling $model::loadData() works as expected.

    The fact that the $this->model::loadData() call doesn't work might have to do we precedence between the :: and -> operators, but I can't say for sure as there's no reference to these in the official documentation.

    For operators of equal precedence, left associativity means that evaluation proceeds from left to right, and right associativity means the opposite. For operators of equal precedence that are non-associative those operators may not associate with themselves. So for example, the statement 1 < 2 > 1, is illegal in PHP. Whereas, the statement 1 <= 1 == 1 is not, because the T_IS_EQUAL operator has lesser precedence than the T_IS_SMALLER_OR_EQUAL operator.

    from PHP Operator Precedence.

    I might be way off here, but from the behaviour of your code I'd say they're either the same precedence or :: has a higher precedence than ->, hence not usable together as you intended.

    UPDATE

    It confirms, the :: operator has higher precedence that -> therefore PHP interprets this:

    $this->model::loadData();
    

    as

    ($this->(model::loadData()));
    

    Check the answer for the $var::staticfunction() OK but $this->var::staticfunction() NOT. What's the rationale? question.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题