PHP – Calling Use from another Function

I have a function

function readExcel(){
require_once 'SimpleXLSX.php';
use Shuchkin\SimpleXLSX;

if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
    print_r( $xlsx->rows() );
} else {
    echo SimpleXLSX::parseError();

But when I call readExcel() I got error

Parse error: syntax error, unexpected token "use"

Help please..

>Solution :

You can’t do this. From the manual:

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

A specific example of a use inside a function call (what you are attempting to do) is shown as illegal.

Leave a Reply