不在对象上下文中时使用$ this-Laravel 4 PHP 5.4.12 [英] Using $this when not in object context - Laravel 4 PHP 5.4.12

查看:113
本文介绍了不在对象上下文中时使用$ this-Laravel 4 PHP 5.4.12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Using $this when not in object context

我刚刚对此问题进行了研究,发现的答案全都与PHP版本有关,但我的版本为5.4.可能是什么问题?

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?

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

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;
  }

推荐答案

使用静态方法时,不能使用$this.静态方法不知道对象状态.您只能使用self::引用静态属性和对象.如果要使用对象本身,则需要感觉自己不在类之列,因此需要创建一个对象的实例,但这将无法理解对象中之前发生的事情. IE.如果某些方法将属性$_x更改为某个值,则在重新实例化该对象时,将失去该值.

You cannot use $this when 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 $_x to some value, when you reinstance the object, you will lose this value.

但是,您可以这样做

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

您也可以将非静态方法称为静态self::method(),但是在较新版本的PHP中,您会收到错误消息,因此最好不要这样做.

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.

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

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

因为静态方法可以在没有对象实例的情况下被调用 创建后,伪变量$ this在方法内部不可用 声明为静态

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


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

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

这篇关于不在对象上下文中时使用$ this-Laravel 4 PHP 5.4.12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆