0

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().

machinateur
  • 502
  • 5
  • 10
  • if you require the file out-side the anonymous function's scope, its accessible to the developer in that environment either way so what are you trying to achieve? – Jaquarh Dec 14 '16 at 11:49
  • @KDOT Let me edit this, I've found out, that this not a problem related to Slim itself. I is caused by the fact that the "use SomeClass as \Another" is not recognized by the code included using "require()" – machinateur Dec 14 '16 at 11:51

2 Answers2

4

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • I don't know how you worded that so easily... +1 – Jaquarh Dec 14 '16 at 12:00
  • Thanks! I just found another question and apparently I just didn't know what exactly I was searching for in the first place. If I think about it, it makes perfect sense to handle it this way – machinateur Dec 14 '16 at 12:05
0

is not recognized by the code - Slim is not a compiler, it is a framework.

The code will execute to the PHP compiler standards, therefore using the default.

use SomeNamespace\SomeClass as SomePrefix;

Will work no matter what you're trying to achieve. The require_once() method loads your PHP file holding the class, which I assume doesn't have a custom namespace since you're targeting the \ directory.

If you're working inside a class then this might be your issue, your code should run similar to this after using the require_once() to load in the ParseMarkdownClass file.

use ParseMarkdownClass as Md;

class SomeClass
{
    public $container;

    public function __construct()
    {
        $this->container = (object)['m' => function() {
            return new Md();
        }];
    }
}

(new SomeClass())->$container->m()->...
Jaquarh
  • 6,493
  • 7
  • 34
  • 86