dongyou4411 2012-08-22 21:45
浏览 79
已采纳

PHP / Codeigniter无法重新声明类 - require_once和class_exists使我失败

I've run into a bewildering problem in our CodeIgniter-powered website. One PHP class - TChild - will intermittently start throwing "cannot redeclare class" errors and breaking every page that uses it. The lengths I've gone to to ensure that the declaring code runs only once have gotten frankly ridiculous, but to no avail. The top of tchild.php currently looks like this:

(!( isset( $GLOBALS['tchild_counter'] ) ))
    ? $GLOBALS['tchild_counter'] = 1 
    : $GLOBALS['tchild_counter']++ ;
log_message("info", "tchild has been included " . $GLOBALS['tchild_counter'] . " times");

if($GLOBALS['tchild_counter'] != 1) {
    log_message("info", "STOP RUNNING TWICE");
} else {

if ( ! defined('BASEPATH')) {
    log_message("error", "BASEPATH not set; no direct script access allowed");
    exit('No direct script access allowed');
}
log_message("info", "Basepath is fine, checking if tchild exists (proc".getmypid().")");
log_message("info", "why does that last line seem to run twice?");
if ( ! class_exists('TChild')) {
log_message("info", "TChild does not exist, creating it");
if(class_exists('tchild')) {
    log_message("info", "tchild apparently exists?");
} else {
    log_message("info","okay, I'm extra-sure that tchild doesn't exist");
}

class TChild extends ActiveRecord\Model {

It's a gigantic pile of... something, but at least it should ensure that TChild is only defined once, right? Well, no. Mostly, it works fine, but occasionally the server will get into some strange state where it decides, despite all evidence to the contrary, that TChild is being declared twice. From the extensive logging statements I've added, I'm fairly certain that tchild.php is only included by a single require_once to begin with, and it definitely doesn't get to the class definition more than once. (It does, however, spit out two "STOP RUNNING TWICE" log entries, even when things are running fine. I don't know why, but at least that doesn't break anything.)

Once it starts failing, it will continue to break everything for (usually) a few minutes, then fix itself for reasons as mysterious as the initial failure.

I have no idea what's going on here. Googling around led me to try adding apc.enabled = 0 to my php.ini, but it hasn't made a difference in the crash or in performance. (I don't think it was enabled to begin with, but it was worth a shot.)

Update:

Aha, there was a require statement in a third-party library that was including the file more than once. I still don't know why the class was being redeclared despite all the if/else blocks I had the declaration wrapped inside, but at least things seem to be working now.

  • 写回答

2条回答 默认 最新

  • dongzhang3482 2012-08-22 23:02
    关注

    Rather than just echoing the fact that the file is included more than once, get PHP to tell you where it's coming from:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私