簡體   English   中英

Kohana:不能重新宣布上課

[英]Kohana: Cannot redeclare class

無法在Kohana開始項目。 我已經從github克隆了它,而不是在配置文件中設置我的數據庫信息並得到錯誤:無法重新聲明類。

我有兩種方法自動加載功能。

public static function auto_load($class, $directory = 'classes')
    {
        // Transform the class name according to PSR-0
        $class     = ltrim($class, '\\');
        $file      = '';
        $namespace = '';

        if ($last_namespace_position = strripos($class, '\\'))
        {
            $namespace = substr($class, 0, $last_namespace_position);
            $class     = substr($class, $last_namespace_position + 1);
            $file      = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR;
        }

        $file .= str_replace('_', DIRECTORY_SEPARATOR, $class);

        if ($path = Kohana::find_file($directory, $file))
        {
            // Load the class file
            require_once $path;


            // Class has been found
            return TRUE;
        }

        // Class is not in the filesystem
        return FALSE;
    }

    /**
     * Provides auto-loading support of classes that follow Kohana's old class
     * naming conventions.
     *
     * This is included for compatibility purposes with older modules.
     *
     * @param   string  $class      Class name
     * @param   string  $directory  Directory to load from
     * @return  boolean
     */
    public static function auto_load_lowercase($class, $directory = 'classes')
    {
        // Transform the class name into a path

        $file = str_replace('_', DIRECTORY_SEPARATOR, strtolower($class));

        if ($path = Kohana::find_file($directory, $file))
        {
            // Load the class file
                require_once $path;


            // Class has been found
            return TRUE;
        }
        // Class is not in the filesystem
        return FALSE;
    }

我試過在require()之前添加class_exists()但它不起作用我應該怎么做才能啟動一個項目?

可能需要的類已包含在其他文件中。 我不知道你究竟是怎么檢查類碰撞問題的,但你應該做的事情如下:

function include_quietly($file) {
 $conflicts = false;
 $txt = file_get_contents($file);
 preg_match_all("#class\s+(\w+)\s*{#muis", $txt, $matches, PREG_SET_ORDER);

 foreach ($matches as $m) {
   if (class_exists($m[1])) {
     $conflicts = true;
     break;
   }
 }

 if (!$conflicts)
   include_once $file;
 else
   echo "include conflicts in file {$file}\n";
}

include_quietly($path);

暫無
暫無

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

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