簡體   English   中英

無法重新聲明課程+找不到課程?

[英]Cannot redeclare class + class not found?

今天,我在使用PHP時遇到了一些非常奇怪的錯誤。

我為A / B測試編寫了一個小類,並想在我的php文檔中創建一個新實例:

if(!class_exists('ab_tester')) include('../lib/php/ab_tester.php');
$ab = new ab_tester($user['id']);

有人認為這應該可以解決問題,但是php表示:

Fatal error: Cannot redeclare class ab_tester in [PATH_TO_PHP]ab_tester.php on line 10

為什么會這樣?

注意:ab_tester.php的第10行如下所示:

class ab_tester {

如果我將include行排除在外,創建ab_tester的新實例,它將吐出:

Fatal error: Class 'ab_tester' not found in [PATH_TO_PHP]returning.php on line 25

現在我該怎么做? 如果我嘗試導入它,它已經存在,如果不導入,它會丟失。

以及ab_tester.php中的前9行代碼包含什么?

我敢打賭那里還有另一個class ab_tester (或者無論如何在include中)

編輯:

另一個可能的解釋是,您稍后將在returning.php的代碼上再包含ab_tester.php 因此,即使您在此特定行中使用include_once ,第二個調用仍然只是一個include ...

怎么樣:

include_once('../lib/php/ab_tester.php');
$ab = new ab_tester($user['id']);

嘗試自動加載類,而不是使用包含?

function __autoload($class_name){
    $class_name = strtolower($class_name);
    $path = "../includes/{$class_name}.php";
    if(file_exists($path)){
        require_once($path);
    }else{
        die("The file {$class_name}.php could not be found.");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM