PHP:$这不是一个对象

发布于 2024-11-16 05:18:03 字数 2601 浏览 2 评论 0原文


我在我们的一个网站上发现了这个非常奇怪的错误。该网站已经正常工作了大约一年,但是昨天突然开始抛出“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文