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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改