> Drupal 8 >

Course Overview

Login to bookmark this course

PHP Namespaces in Under 5 Minutes

Unravel the mystery of PHP namespaces in 5 minutes! Learn `namespace`, `use`, and `\` usage.

  • 4106 students
  • EN/ES Captions
  • EN/ES Script
  • Certificate of Completion

Your Guides

About this course

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

Next courses in the Drupal 8: The OO Prerequisites section of the Drupal 8 Track!

40 Comments

Sort By
Login or Register to join the conversation
Default user avatar Troy Close 6 years ago

Brilliant. I just wasted my time reading two different tutorials on namespaces and still didn't understand it. Watched your video and got it immediately. Thanks Leanna and Ryan for uncomplicating this subject.

10 | Reply |

So nice, thanks Troy :)

3 | Reply |

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

3 | Reply |
Default user avatar sancoLgates 6 years ago

great explanation, thanks :)
i get it now!

2 | Reply |
Default user avatar Arturas Lapinskas 6 years ago

guess i'm not the first, who say that this titorial is great! Thank you!

1 | Reply |
Default user avatar Emmanuel Higgins 6 years ago

Wish documentation was like this.

1 | Reply |
Default user avatar Mickaƫl LP 6 years ago

Waow ! These 2 minutes were incredibly efficient (in my case).
Thank you for this course, I feel like I've just discovered my new Bible with knpuniversity :)

1 | Reply |
Adrian-I avatar Adrian-I 8 months ago

Wow, this is amazing! I just understood in 5 minutes in a pleasant and fun way. You guys are simply the best.

| Reply |

I can't watch this video, i was looking forward to learning more about PHP Namespaces :(

| Reply |

Hey Aina,

This course has only a single video that is completely free: https://symfonycasts.com/screencast/php-namespaces/namespaces - Could you tell us more about what exactly does not work for you? Could you try again, please? Or try to open that video in Chrome Incognito mode? I believe that was a temporary network issue, so it should work for you now unless you have some restrictions to Vimeo videos in your country since all our videos are hosted there.

Cheers!

| Reply |

Last challenge not working even after copy-pasting correct answer :( Even when using "exit" just after PHP opening tag in Lunch.php file. Looks like something is ... stuck? in cache or something?

| Reply |

Hey Adam,

My apologies for the inconvenience, I think you're right about the cache because the challenge it's working for me. You can wait like 15 minutes to let the cache refresh and try again.
Please, let me know if the problem persists.

| Reply |

Hi!

yep, that was a cache problem :) Now it's works fine. Thanks!

| Reply |

Very clear! Thank you very much for that tutorial! :) <3

| Reply |
Hicham Z. avatar Hicham Z. 6 years ago

easy, fast and great !!!!
Thanks

| Reply |
Default user avatar å½­å¤©ę™“ 6 years ago

Awesome! Really save my time!

| Reply |
Default user avatar BIBIN JOHN 6 years ago

The best php namespace tutorial.... Great... keep it up...

| Reply |

Hey, thank you!

| Reply |
Default user avatar Sajeepan Yogenthiran 6 years ago

useful tip :-) Thank you.

| Reply |
Default user avatar Jeric Realubit 6 years ago

Sweet As! Thank you!

| Reply |

Hey Jeric,

We're glad you like it! Honestly, this is my favorite tutorial :p

Cheers!

1 | Reply |
Default user avatar Mr TK 6 years ago

And why do you need to require a file when namespacing? Isn't namespacing intended to do away with requiring?

| Reply |

Hey Mr TK

In this case we are using "require" in a file (some-other-file.php) that doesn't define a namespace, the one who has a namespace is the Foo class, so if you want to create a Foo object in other file, you have two options

1) In that file, define it's namespace and write a "use" statement that points to the "Foo" class

2) Don't define a namespace and use "require" or "include" that points for that class

Cheers!

| Reply |
Default user avatar R0bertinski 6 years ago

I am used to listen videos in english, but is very complicate to understand this videos, because she speak very fast.

| Reply |

Hey R0bertinski!

I wonder if (English) sub-titles would help? I want to do the most useful thing for our non-native English-speaking friends :).

Thanks!

3 | Reply |
Default user avatar Alberto Maturano weaverryan 6 years ago

Yes, It would help :D

1 | Reply |
Default user avatar Mickaƫl LP weaverryan 6 years ago

It think it would actually help !
I generally have the same 'problem'.
But not putting sub-titles also makes us focusing more on the screen and the intonation of the voice.

So having the choice of activating sub-titles would be a benefit (in my opinion).

1 | Reply |
Default user avatar ENetArch 6 years ago

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.

| Reply |

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!

4 | Reply |
Default user avatar TuConciencia weaverryan 6 years ago

120 seconds + this explanation and I'm ready to go =)
Thank you!

| Reply |
Default user avatar Braxton Overby 6 years ago

I got myself ready to sit down and read/watch for several minutes on how to properly use namespaces in PHP. I appreciate how easy this tutorial was to follow.

| Reply |
Default user avatar Jimmy James Atauje Hidalgo 6 years ago

Very nice tutorial

| Reply |

Thanks

| Reply |
Default user avatar Zoe Abe 6 years ago

Quite helpful, many thanks! I realized it before I know I realized it (does that make sense? :P)
But do you happen to have a 0.75x speed playback option? 120 seconds sound like a rush to me~~ :-D

| Reply |

Ha, 120 seconds is definitely a rush! You can't slow down the video yet, but we're adding speed control really soon :)

1 | Reply |
Default user avatar spudgun 6 years ago

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

| Reply |

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/sc.... Again, it's actually unrelated to namespaces.

Cheers!

3 | Reply |
Default user avatar Janak 6 years ago

excellent explanation. Thanks!!!

| Reply |
Default user avatar Jerome Covington 6 years ago

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

| Reply |
Default user avatar Century Media 6 years ago

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!

| Reply |

Delete comment?

Share this comment

astronaut with balloons in space

"Houston: no signs of life"
Start the conversation!