PHP Namespaces in 120 Seconds

What you will learn

Still not quite comfortable with PHP 5.3 namespaces? No problem! Give us 120 seconds and we'll introduce you to all the crazy characters ("namespace", "use" and "\") and show you how they work.

Start Course Add to Cart

Leave a comment!

  • 2014-07-29 Century Media

    Great overview. Only thing I think it could use is to mention that the main use of namespaces is to stop method names conflicting. Aside from that great job!

  • 2014-06-04 weaverryan

    Hi there!

    Actually, namespaces and autoloading are 2 separate things, though in practice, due to PSR-0 autoloaders, they are quite related.

    Namespaces are all about basically making class names really long, and then giving us a "use" statement to help shorten things. On its own, it doesn't help with removing the require/include statements. So you're right, on their own, namespaces actually *add* code and make things longer. Really, namespaces weren't created for you and I, they were created for library authors to avoid name collisions (like you mentioned). For us, they just make things longer :).

    But in practice (and you're totally right about this part), if you use namespaces properly, AND you use a PSR-0/PSR-4 autoloader (like the one provided in Composer), then you'll be able to remove your require/include statements. The autoloader basically leverages namespaces to do this, but namespaces on their own don't allow you to remove those ugly require/include things from your code.

    I hope that clarifies - it's confusing because namespaces *seem* to make include/require statements no longer necessary (because in practice, this is true). But in reality, on their own, they just make class names longer and mean more typing.

    Cheers!

  • 2014-06-04 ENetArch

    So, why wasn't the required_once function removed from your example? If that function is required to import your classes, then there is no purpose in updating code to include namespaces, since there is no benefit in reducing code, instead, you've increase code.

    The purpose of adding a namespace, not only has to reduce class name clashes, but also automate the finding and binding of classes somehow, otherwise there is no use to using namespaces.

  • 2014-02-12 Jerome Covington

    That was awesome. Thanks for this. Namespaces and object-oriented code in PHP are bringing me back.

  • 2014-01-23 weaverryan

    Hi there!

    Namespaces and using the include/require statements don't do the same thing - it's one of the trickiest parts of learning namespaces. You still need to include/require any file before using the class inside of it (unless you use an autoloader, like the one Composer gives you). Namespaces have nothing to do with that part.

    The point of namespaces is, basically, to give you a systematic way to make your class names longer. When you have the `Acme\Tools` namespace, the full class name ends up being `Acme\Tools\Foo`. Why is this useful? Well, on its own, it's not :). It's really so that 2 3rd-party library authors an choose 2 different namespaces so that their class names don't conflict. Namespaces really help to serve that purpose: to avoid conflicts between libraries.

    If you want to see a little bit more on the autoloading side of things (i.e. removing the need for require/include) - check out http://knpuniversity.com/scree.... Again, it's actually unrelated to namespaces.

    Cheers!

  • 2014-01-23 spudgun

    You explained what namespaces do, but you didn't explain why I might need them. You say "This is just like referring to a file by its absolute path.", so ... why don't you just do that?

    include('Acme/Tools/Foo.php');

  • 2013-12-08 JC

    Wow... you really did it in 120 secs :-)

Tutorial Authors

Ryan weaver Leanna Pelham

Videos In this Tutorial

Back to the screencasts list