不在对象上下文中时使用 $this - Laravel 4 PHP 5.4.12

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21349732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:54:41  来源:igfitidea点击:

Using $this when not in object context - Laravel 4 PHP 5.4.12

phplaravellaravel-4

提问by Fabrizio Fenoglio

I was attemping to access on my instance on the constructor with the variable $this; In all other method it seem work good when i call $this->event->method()but on this method it throw me an error

我试图使用变量 $this 在构造函数上访问我的实例;在所有其他方法中,当我调用时它似乎工作良好,$this->event->method()但在此方法中它抛出了一个错误

Using $this when not in object context

不在对象上下文中时使用 $this

I just did a research about this issue and the answers i found was all about the version of PHP but i have the version 5.4. what can be the issue?

我刚刚对这个问题进行了研究,我发现的答案都是关于 PHP 版本的,但我的版本是 5.4。可能是什么问题?

This is the method that i try to call the instance.

这是我尝试调用实例的方法。

// all protected variable $event , $team , $app
function __construct(EventTeamInterface $event,TeamInterface $team) {
    $this->event = $event;
    $this->team = $team;
    $this->app = app();
  }

  /**
  * @param $infos array() | 
  * @return array() | ['status'] | ['msg'] | ['id']
  */
  public static function createEvent($infos = array()){
      $create_event = $this->event->create($infos);
        if ($create_event) {
            $result['status'] = "success";
            $result['id'] = $create_event->id;
        } else {
            $result['status'] = "error";
            $result['msg'] = $create_event->errors();
        }

        return $result;
  }

回答by Royal Bg

You cannot use $thiswhen you are in static method. Static methods are not aware of the object state. You can only refer to static properties and objects using self::. If you want to use the object itself, you need to feel like you are out of the class, so you need to make instance of one, but this will fail to understand what has happened before in the object. I.e. if some method changed property $_xto some value, when you reinstance the object, you will lose this value.

$this在静态方法中不能使用。静态方法不知道对象状态。您只能使用self::. 如果你想使用对象本身,你需要感觉你是在类之外,所以你需要创建一个实例,但这将无法理解对象之前发生了什么。即,如果某个方法将属性更改$_x为某个值,则当您重新实例化该对象时,您将丢失该值。

However, in your case you can do

但是,在您的情况下,您可以这样做

$_this = new self;
$_this->event->create($info);

You can also call non static methods as static self::method()but in newer versions of PHP you will get errors for this, so it's better to not do it.

您也可以将非静态方法称为静态方法,self::method()但在较新版本的 PHP 中,您会因此而出错,因此最好不要这样做。

The information about it, you can find in the official php documentation: http://www.php.net/manual/en/language.oop5.static.php

关于它的信息,可以在php官方文档中找到:http: //www.php.net/manual/en/language.oop5.static.php

Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static

由于静态方法无需创建对象的实例即可调用,因此伪变量 $this 在声明为静态的方法中不可用



Calling non-static methods statically generates an E_STRICT level warning.

静态调用非静态方法会生成 E_STRICT 级别警告。