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 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥15 看一下OPENMV原理图有没有错误
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题