user3919287
user3919287

Reputation:

Calling static autoloaded class without namespace

I was wondering if it's possible to use a static PHP class (or a method of that class, should I say) without calling it via the namespace ala laravel?

I realise this is conflicting with the nature of namespaces, but I was mainly wanting to use them the same as Laravel does, where I can just call Example::method('test') rather than \example\example\Example::method('test')

Upvotes: 3

Views: 921

Answers (1)

user1842104
user1842104

Reputation: 181

At the top of your file, declare a use statement which creates an alias:

use example\example\Example;

Example::method('test'); //resolves to example\example\Example::('test')

You can even use a different name with 'as':

use example\example\Example as MyAlias;

MyAlias::method('test'); //resolves to example\example\Example::('test')

See http://php.net/manual/en/language.namespaces.importing.php for more information.

Note: This is not how laravel facades work but I think it is what you are looking for. If you want something more automatic then aliasing you will have to incorporate some logic into your autoloader.

Upvotes: 2

Related Questions