Codeigniter模型致命错误:不在对象上下文中时使用$ this [英] Codeigniter Model Fatal error: Using $this when not in object context
问题描述
我对一些适用于开发但不适用于生产的代码有一点问题。这很奇怪,因为所有其他代码都起作用(或似乎起作用)。
I have a little issue with some code that works on development but not on production. It is weird, because all the other code works (or seems to).
整个错误如下:
致命错误:在
/[snip]/application/modules/manage_plugins/models/Manage_plugins.php
中不在对象上下文中时,使用$ this遇到PHP错误
Fatal error: Using $this when not in object context in /[snip]/application/modules/manage_plugins/models/Manage_plugins.php on line 6 A PHP Error was encountered
严重性:错误
消息:在对象上下文中不使用$ this
Message: Using $this when not in object context
文件名:models / Manage_plugins.php
Filename: models/Manage_plugins.php
行号:6
回溯:
从我发现的其他类似问题中,这是由于人们试图在其中使用 $ this静态上下文-我认为情况并非如此。
From the other similar issues I could find, it was due to people attempting to use "$this" in a static context - I don't believe that is the case for me.
这里是manage_plugins构造函数,第6行(错误行)是该构造函数的第一行:
Here is the manage_plugins constructor, with line 6 (error line) being the first in the constructor:
class Manage_plugins extends CI_Model {
public function __construct() {
$this->mediator->attach("manage_load", function($name, $data) { $this->on_manage_load(); });
$this->load->model("automediator");
}
}
正在由以下文件加载代码(从未明确调用过):
It is being loaded by the following code (and never called explicitly):
$CI =& get_instance();
$CI->load->model("manage_plugins/manage_plugins");
有人知道为什么会这样吗?
Does anyone know why this is happening?
推荐答案
感谢rexmarc,我能够解决此问题,并通过 use
-ing a使类似的结构在PHP 5.3上工作对象 $ this
在匿名函数中的副本。
Thanks to rexmarc, I was able to workaround the issue and make a similar structure work on PHP 5.3 by use
-ing a copy of the object $this
in the anonymous function.
我更改了以下内容:
class Manage_plugins extends CI_Model {
public function __construct() {
$this->mediator->attach("manage_load", function($name, $data) { $this->on_manage_load(); });
$this->load->model("automediator");
}
}
成:
class Manage_plugins extends CI_Model {
public function __construct() {
$me =& $this;
$this->mediator->attach("manage_load", function($name, $data) use($me) { $me->on_manage_load(); });
$this->load->model("automediator");
}
}
对此的另一种解决方案是:
Another solution for this could have been:
class Manage_plugins extends CI_Model {
public function __construct() {
$this->mediator->attach("manage_load", [$this, 'on_manage_load']);
$this->load->model("automediator");
}
}
出现此问题是因为在PHP中5.4之前的版本中, $ this
在匿名函数中不可用。
The issue was occurring because in PHP versions prior to 5.4, $this
was not available in anonymous functions.
5.4.0-匿名函数可以使用$ this并进行静态声明
5.4.0 - Anonymous functions may use $this, as well as be declared statically
来源: http:// php。 net / manual / en / functions.anonymous.php
由于开发版本(5.5)和生产版本(5.3)上的PHP版本不同,因此未引起注意。
The issue went unnoticed because of differing PHP versions on development (5.5) and production (5.3).
另请参见: https://stackoverflow.com/a/19432335/ 3649573
这篇关于Codeigniter模型致命错误:不在对象上下文中时使用$ this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!