PHP:$这不是一个对象
我在我们的一个网站上发现了这个非常奇怪的错误。该网站已经正常工作了大约一年,但是昨天突然开始抛出“ramdom”500 错误,例如 $this 不是一个对象,或者包含随机失败(这不应该在任何情况下失败)案件)。
代码示例:
//class Enviroment
219 public function run()
220 {
221 //vložit prostředí
222 if (class_exists('CONFIG')) {
223 $this->dir_basic = Config::$DirBasic;
224 }
225 include_once($this->dir_basic . 'web/libsf/enviroment/types/' . ($this->mode == 'dev' ? 'dev' : 'prod') . '.php');
226
227 //vytvořit třídu prostředí
228 $enviroment_class = 'Enviroment' . $this->mode;
229 $this->enviroment = new $enviroment_class($this);
230 $this->enviroment->pre_libs();
231
232 //načíst knihovny
233 $this->load_libs();
234
235 //zavolat inicializační metody po načtení knihoven
236 $this->enviroment->post_libs();
237
238 $this->init_vars();
239 }
这个类通过这个正确启动(这个确切的代码在我们的许多网站上都有效,只是这个突然停止工作):
$aplication = Enviroment::get_aplication('prod');
$aplication->run();
这会引发以下错误(注意行号):(
Notice: Trying to get property of non-object in /var/www/vhosts/e/example.cz/data/www/web/libsf/enviroment/enviroment.php on line 225
Notice: Trying to get property of non-object in /var/www/vhosts/e/example.cz/data/www/web/libsf/enviroment/enviroment.php on line 228
Notice: Trying to get property of non-object in /var/www/vhosts/e/example.cz/data/www/web/libsf/enviroment/enviroment.php on line 230
Fatal error: Call to a member function pre_libs() on a non-object in /var/www/vhosts/e/example.cz/data/www/web/libsf/enviroment/enviroment.php on line 230
我相信第 225 行的 include 失败,因为 $this 是“不是对象”...)。 我在 CentOS 5.5 上运行 PHP5.3.3。 当我重新启动 apache 时,一切似乎都工作正常一段时间(五分钟,两小时,七秒......),然后它再次开始抛出这些错误。
任何帮助表示赞赏。
编辑: 环境类构造函数:
private function __construct($mode)
{
$this->mode = $mode;
//vygenerovat unikátní hash
mt_srand((double) microtime() * 1000000);
$this->hash = md5(uniqid(mt_rand(), TRUE)) . rand(0, 99999999);
}
Get_application方法:
public static function get_aplication($mode = 'auto')
{
if ($mode == 'auto') {
//nastavit produkční mod
$mode = 'prod';
//rozpoznat typ developerského módu
if ($_SERVER['SERVER_ADDR'] == '127.0.0.1' || preg_match('/^localhost/', $_SERVER['HTTP_HOST']) || $_GET['env_mode'] == 'developer') {
$mode = 'dev';
}
}
return self::$enviroment_obj = new Enviroment($mode);
}
I came across this very weird bug on one of our websites. The website has worked properly for about a year now, however yesterday all of the sudden it started throwing "ramdom" 500 errors, statig for example that $this is not an object, or an include fails randomly (which shoud not have failed in any case).
A sample of code:
//class Enviroment
219 public function run()
220 {
221 //vložit prostředí
222 if (class_exists('CONFIG')) {
223 $this->dir_basic = Config::$DirBasic;
224 }
225 include_once($this->dir_basic . 'web/libsf/enviroment/types/' . ($this->mode == 'dev' ? 'dev' : 'prod') . '.php');
226
227 //vytvořit třídu prostředí
228 $enviroment_class = 'Enviroment' . $this->mode;
229 $this->enviroment = new $enviroment_class($this);
230 $this->enviroment->pre_libs();
231
232 //načíst knihovny
233 $this->load_libs();
234
235 //zavolat inicializační metody po načtení knihoven
236 $this->enviroment->post_libs();
237
238 $this->init_vars();
239 }
This class is initiated properly, throught this (this EXACT code works on MANY of our websites, just this one stopped working all of the sudden):
$aplication = Enviroment::get_aplication('prod');
$aplication->run();
This throws the following errors (note the line numbers):
Notice: Trying to get property of non-object in /var/www/vhosts/e/example.cz/data/www/web/libsf/enviroment/enviroment.php on line 225
Notice: Trying to get property of non-object in /var/www/vhosts/e/example.cz/data/www/web/libsf/enviroment/enviroment.php on line 228
Notice: Trying to get property of non-object in /var/www/vhosts/e/example.cz/data/www/web/libsf/enviroment/enviroment.php on line 230
Fatal error: Call to a member function pre_libs() on a non-object in /var/www/vhosts/e/example.cz/data/www/web/libsf/enviroment/enviroment.php on line 230
(I believe the include on line 225 fails because $this is "not an object"...).
I'm running PHP5.3.3 on CentOS 5.5.
When I restart apache, everything seems to work all right for a while (five minutes, two hours, seven seconds...) and then it starts throwing these errors again.
Any help appreciated.
EDIT:
Enviroment class constructor:
private function __construct($mode)
{
$this->mode = $mode;
//vygenerovat unikátní hash
mt_srand((double) microtime() * 1000000);
$this->hash = md5(uniqid(mt_rand(), TRUE)) . rand(0, 99999999);
}
Get_application method:
public static function get_aplication($mode = 'auto')
{
if ($mode == 'auto') {
//nastavit produkční mod
$mode = 'prod';
//rozpoznat typ developerského módu
if ($_SERVER['SERVER_ADDR'] == '127.0.0.1' || preg_match('/^localhost/', $_SERVER['HTTP_HOST']) || $_GET['env_mode'] == 'developer') {
$mode = 'dev';
}
}
return self::$enviroment_obj = new Enviroment($mode);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论