dongsheng8158 2013-12-25 00:29
浏览 69
已采纳

函数require_once不会根据自定义函数显示异常消息

The function below doesn't seem to work correctly. In the function load_module I am using the require() function in PHP to get the file from Modules Folder.

What am trying to do is, check if file is in the modules folder, if its exist then run the require_once() function. When I try with the file on the modules folder its works great, then what I did was removed the file and then tried again it gave me this error:

Warning: require_once(xxxxxxxxxxxxxxxxxxxxxxx): failed to open stream: No such file or directory in D:\xampp\htdocs\projects\hoplate\admin\components\template.php on line 4

Whereas it should have showed this exception error message:

There was an error, It seems that the moudle doesn't exist in it's location xxxx

Function load_module

function load_module( $_module_file, $require_once = true ) {
        if ( $require_once )
            if ( require_once( $_module_file ))
                require_once( $_module_file);
            else
                return "There was an error, It seems that the moudle doesn't exist in it's location $_module_file";
        else
            if( require( $_module_file ) )
                require( $_module_file );
            else 
                return "There was an error, It seems that the moudle doesn't exist in it's location $_module_file";
    }

Function get_module

/* Get Modules */
            function get_module( $_module_slug ){
                load_template( ADMIN_PATH . '/modules/' . $_module_slug . '.php');
            }

More Information I have also tried using the file_exists() function but it still doesn't seem to work.

  • 写回答

2条回答 默认 最新

  • duanfu3634 2013-12-25 00:43
    关注

    According to the documentation

    require[_once] is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error

    Your if check is not only returning false, but also causing a fatal error which stops execution, so it never gets to the else

    You should either use include instead (which only causes a WARNING) or file_exists to check if the module exists... since you are handling the error yourself there is no benefit to using require over include. Also, just for better practice I would recommend throwing an exception instead of returning an error string.

    Full code:

    if ( file_exists( $_module_file ))
      include_once( $_module_file );
    else
      throw new Exception("error...");
    

    Or to simplify things, if you're ignoring WARNING level errors

    if( !include_once( $_module_file ))
      throw new Exception("error...");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?