Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')


1 Answers

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.

like image 144
user1842104 Avatar answered Jun 29 '26 16:06

user1842104



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!