PHP: $this is not an object
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_HO开发者_Python百科ST']) || $_GET['env_mode'] == 'developer') {
$mode = 'dev';
}
}
return self::$enviroment_obj = new Enviroment($mode);
}
精彩评论