16

This is what I have at hand:

//Person.php
namespace Entity;

class Person{


}

User file:

//User.php
use Entity\Person;

$person = new Person;

Here, it fails if I don't include the Person.php file. If I include it, the everything works fine. Do I absolutely require to include the file even when using namespaces? If at all we need to include/require files, then how can namespaces be effectively used? Also, can we maintain folder structure by nesting namespaces?

Script47
  • 14,230
  • 4
  • 45
  • 66
Rutwick Gangurde
  • 4,772
  • 11
  • 53
  • 87

3 Answers3

9

The answer to your question is "yes and no".

Indeed the code implementing class Person has to be included, otherwise the class is not defined and cannot be used. Where should the definition come from, when the code is not included? The php interpreter cannot guess the classes implementation. That is the same in all programming languages, by the way.

However there is something called Autoloading in php. It allows to automatically include certain files. The mechanism is based on a mapping of class names to file names. So in the end it boils down to php searching through a folder structure to find a file whos name suggests that it implements a class currently required in the code it executes.

But don't get this wrong: that still means the file has to be included. The only difference is: the including is done automatically, so without you specifying an explicit include or require statement.

Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Yes, autoloading sounds sensible. But if I have say 50 files, all with their own classes, they need inclusion as well? – Rutwick Gangurde May 30 '14 at 14:06
  • 1
    Simple answer: yes, they all have to be included. Well, those that define classes that are actually used, that is. It does not matter if the inclusion is done manually or automatically, but they have to be included. Actually it is considered good style to create a separate file for each separate class. Take a look at the documentation (I linked it in the answer) to read about the naming conventions, so that autoloading works. – arkascha May 30 '14 at 14:07
  • Great. I'm looking into autoloading now. Just one more question, how do I call that file which autoloads everything? – Rutwick Gangurde May 30 '14 at 14:12
  • You do not need a special file for that. The interpreter interprets a script that was requested, for example by a http request to a http server. Inside that script, a class is used. That is when the interpreter realizes that this class is not (yet) defined and starts the auto loading mechanism to include the file implementing the class. Only if such a file cannot be found, then an error is thrown. In short: you think too complex. Just read the documentation. – arkascha May 30 '14 at 14:15
  • 1
    Okay, so, I just define the autoloading in a file autoload.php, and let it reside alongside my index.php file. Then I can go ahead and use the namespace/class, without even requiring the autoload.php? – Rutwick Gangurde May 30 '14 at 14:17
4

Yes, you need to include every file. A very good example can be found here on effective usage of namespaces.

gnagy
  • 410
  • 3
  • 9
  • 1
    I see in Zend, they use namespaces, but none of the files are included/required. So, how do they do it? Some kind of 'automatically include' method? Pardon my illiteracy in this matter! – Rutwick Gangurde May 30 '14 at 14:04
  • Exactly, all frameworks, and cmfs are using some kind of autoloader method. The Zend itself uses this: http://framework.zend.com/manual/1.12/en/zend.loader.autoloader.html – gnagy May 30 '14 at 14:06
  • 1
    Aha! So I need to learn auto loading! Thanks! Now it makes some sense. – Rutwick Gangurde May 30 '14 at 14:07
  • That's right. I would suggest you to start at this stackowerflow post, it seems like a good solution: http://stackoverflow.com/a/3642440/3616785 – gnagy May 30 '14 at 14:11
  • Yes. I saw it too, but even there, the file is required. How is that? How do I use the file that autoloads everything? – Rutwick Gangurde May 30 '14 at 14:13
1

With PSR-0 autoloading, the namespace has to be the same as the folder in which the class is, file the filename has to be the same as the classname. This gives you very simple and effective autoloading with composer for example.

automaton
  • 1,091
  • 1
  • 9
  • 23