简体   繁体   English

非静态方法不应静态调用

[英]Non-Static Method should not be called Statically

I am using a repository pattern and am trying to establish relationships between models. 我正在使用存储库模式,并试图建立模型之间的关系。 When I try to run the store() method (in the controller) which is trying to use the user() method (which establishes the relationship with the Party model), I get the following error message: 当我尝试运行store()方法(在控制器中)试图使用user()方法(该方法与Party模型建立关系)时,收到以下错误消息:

Non-static method Party::user() should not be called statically, assuming $this from incompatible context 假设不兼容的上下文中的$ this,则非静态方法Party :: user()不应静态调用

I don't understand why I get this error when I try to run the user() relationship method, but all of the other methods (including $this->party->all(), $this->party->create($data)), work just fine. 我不明白为什么尝试运行user()关系方法时会收到此错误,但是所有其他方法(包括$ this-> party-> all(),$ this-> party-> create( $ data)),就可以了。

Here is the relevant code: 以下是相关代码:

// PartiesController.php
public function __construct(Party $party){
  $this->party = $party
}

public function store(){
  $data = Input::all();
  $user = Sentry::getUser(); 
  $this->party->user()->create($data);
}

// Party.php
class Party extends Eloquent{
  public function user(){
    return $this->belongsTo('User');
  }
}

// User.php
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;

class User extends SentryUserModel implements UserInterface, RemindableInterface {
  public function party(){
    return $this->hasMany('Party');
  }
}

// PartyRepository.php
namespace repositories\Party;

interface PartyRepository{
  public function all();

  public function findByID($id);

  public function create($input);

  public function user();
}

// EloquentPartyRepository.php
namespace repositories\Party;
use Party;

class EloquentPartyRepository implements PartyRepository{
  public function all(){
    return Party::all();
  }

  public function create($input){
    return Party::create($input);
  }

  public function user(){
    return Party::user();
  }
}

The issue is because you are calling a non-static method in a static context. 问题是因为您要在静态上下文中调用非静态方法。 You may be used to seeing the way Laravel does a lot of this (eg User::find() and the like). 您可能已经习惯于了解Laravel进行大量此类操作的方式(例如User::find()等)。 These, in reality though, are not static calls (a class instance is actually being resolved behind the scenes and the find() method invoked on that instance). 但是,实际上这些并不是静态调用(实际上是在后台解析类实例,并在该实例上调用find()方法)。

In your case, it is just a plain static method call. 在您的情况下,这只是一个普通的静态方法调用。 PHP would allow this, except for the fact that in the method you are referencing $this and PHP doesn't know what to do with it. PHP将允许这样做,但事实是您在方法中引用了$this而PHP不知道如何处理它。 Static method calls, by definition, have no knowledge of any instances of a class. 根据定义,静态方法调用不了解类的任何实例。

My advice would be to inject an instance of your Model class into your repository's constructor, something like this: 我的建议是将Model类的实例注入到存储库的构造函数中,如下所示:

//Class: EloquentPartyRepository
public function __construct(Party $party) 
{
    $this->party = $party;
}

public function user($partyId) 
{
    return $this->party->find($partyId)->user();
}

The Party instance you send to the constructor should not be a record from the database, just an empty instance of Party (ie new Party() ), though I believe if you just add it to the constructor, the IoC should be able to leverage dependency injection and provide you with an instance. 您发送给构造函数的Party实例不应是数据库中的记录,而应该只是Party的空实例(即new Party() ),尽管我相信,如果仅将其添加到构造函数中,则IoC应该能够利用依赖注入,并为您提供实例。

An equivalent implementation is here, that adds a byId method: 这里有一个等效的实现,它添加了byId方法:

//Class: EloquentPartyRepository
public function __construct(Party $party) 
{
    $this->party = $party;
}

public function byId($partyId)
{
    return $this->party->find($partyId);
}

public function user($partyId) 
{
    if($party = $this->byId($partyId)) {
        return $party->user();
    }

    return null;
}

I have solved the problem. 我已经解决了问题。 Thank you @watcher and @deczo for your feedback. 感谢@watcher和@deczo的反馈。 Both were very helpful and relevant to this error message. 两者都非常有用,并且与此错误消息相关。

In the end, I only needed to change one line. 最后,我只需要更改一行即可。 I had the sequence of method calls out of order in the store() function. 我在store()函数中无序地调用了方法。 Here is the relevant code. 这是相关的代码。

// PartiesController.php
public function store(){
  $data = Input::all();
  $user = Sentry::getUser(); 
  $user->party()->create($data);
}

In my case, to remove the non-static error and to properly insert the User model into the Party model, I only had to make the aforementioned change. 就我而言,要消除非静态错误并将User模型正确插入Party模型中,我只需要进行上述更改。

I referred to http://laravel.com/docs/eloquent/#inserting-related-models for the appropriate sequence. 我参考了http://laravel.com/docs/eloquent/#inserting-related-models以获取适当的序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM