php導(dǎo)入模塊文件分享

字號:


    本文給大家分享的是php導(dǎo)入模塊文件分享,主要參數(shù)有導(dǎo)入文件路徑字符串,可以用"."代替"/", 導(dǎo)入文件類型的擴展名(帶"."號),也可以是class/inc(簡寫方式), 如果導(dǎo)入成功則返回true,否則返回異常對象,有需要的小伙伴參考下吧。
    代碼很簡單,大家注意看注釋就可以了。
    代碼如下:
    /**
    * 導(dǎo)入模塊文件
    *
    * @param string $classString 導(dǎo)入文件路徑字符串,可以用"."代替"/"
    * @param string $fileType 導(dǎo)入文件類型的擴展名(帶"."號),也可以是class/inc(簡寫方式)
    * @return Exception 如果導(dǎo)入成功則返回true,否則返回異常對象
    *
    * @example
    * importModule('gapi.Account') => include_once('modules/gapi/Account.class.php');
    */
    function importModule($classString, $fileType = 'class')
    {
    $filename = $module_path. strtr($classString, '.', '/');
    switch ($fileType) {
    //導(dǎo)入類文件
    case 'class': $filename .= '.class.php'; break;
    //導(dǎo)入包含文件
    case 'inc': $filename .= '.inc.php'; break;
    //自定義導(dǎo)入文件的擴展名
    default: $filename .= $fileType; break;
    }
    if (is_file($filename))
    {
    include_once($filename);
    }
    else
    {
    exit('class " . strtr($classString, '.', '\\') . '" is not found.');
    }
    }
    以上就是本文分享給大家的代碼了,希望大家能夠喜歡。