5

Is there a solution to having to include all classes individually under a namespace?

My Laravel files are getting huge because I have to keep including a heck load of namespaces... It's pretty awful!

As a temporary solution, why might the following not be working:

namespace.Blah.txt:

use Blah\Blah; 
use Blah\Bloh;

php code:

eval( file_get_contents( "namespace.Blah.txt" );

If I could get this to work, I could evaluate the contents of a file... I do understand it's a bit noob... but... dammit!

3
  • As in autoload all classes with a specific namespace? Commented Apr 29, 2015 at 13:56
  • How many included classes are we talking about? Commented Apr 29, 2015 at 14:16
  • I have about 20 classes at the moment. I pretty much have to include all of them in every controller... I'm really not that fussed about declaring memory for the classes as they are TINY... and they are never auto instantiated so will never be filled. This just isn't a problem for me at this moment in time... If it becomes one, I will go back to the old way... But I'm not understanding why the eval() is not working? Commented Apr 29, 2015 at 14:20

1 Answer 1

7

There isn't, but in PHP 7 you'll be able to

use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo };

As the following RFC has passed:

https://wiki.php.net/rfc/group_use_declarations

EDIT:

Note that too many uses in a class may be a sign of "smell". Isn't that particular class doing too much? Shouldn't you be creating new "base" classes and extending them?

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for that, I wondered if you might then know if I can do the above (eval)... I know it's horrid... But i'm desperate! And it can be changed at some point if it gets to be noob!
Too many uses in a class may be a sign of "smell". Isn't that particular class doing too much? Shouldn't you be creating new "base" classes and extending them?
I'm pretty sure eval will not work in this case, because uses are processed by the compiler. It's only a hint for the compiler to know where to find the class, so if you execute the use from elsewhere it will not work on your current class.
Hah. Thanks for the tip man. Any ideas why you can't evaluate the use keyword?
Because it's not really for evaluation, it's just a hint. You can replace use Foo; Foo::whatever() by \Foo::whatever();, so it's something only to tell PHP where to find that class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.