Search code examples
phpphp-closures

Php require() does not recognize aliased class


So mainly this is caused by my code structure:

File1.php

use \Class1 as Blah;
require 'File2.php';

File2.php

$closure = function ($arg) {
    return new Blah($arg);
};

Somehow the part behind ... as isn't recognized after using require().


Solution

  • Namespace aliases are only valid within the file in which you write the use statement. The aliases do not transfer across file boundaries. If you want to use Class1 as Blah within File2.php, you need to put that statement at the top of File2.php.