函数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.

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...");

For someone else bump into same issue. This is the working solution

<?php
    function load_module( $_module_file, $require_once = true ) {
        if ( $require_once )
            if( !file_exists ( $_module_file ))
                echo "<strong>There was an error</strong>, <br/> It seems that the module doesn't exist in it's location <b>$_module_file</b>";
            else 
                require_once( $_module_file );

        else
            if( !file_exists ( $_module_file ))
                echo "<strong>There was an error</strong>, <br/> It seems that the module doesn't exist in it's location <b>$_module_file</b>";
            else 
                require( $_module_file );
    }

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