1. 45
    Defining & using PHP namespaces
    4m 19s

Defining & using PHP namespaces

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Once you start creating PHP classes, you'll want to organize them into groups of classes. In this lesson, you'll learn how to do just that with namespaces.

Instructor: [0:00] Namespaces are simply a way for you to organize classes. When your code base gets much larger, namespaces can help you manage them. This can also help you out, if you have multiple classes with the same name.

[0:13] By default, PHP uses one single global namespace, so if you create two classes with the same name, they will actually conflict with one another.

[0:23] However, by defining namespaces in your classes, it helps compartmentalize your app into specific organizational groups. The same class name can actually be used multiple times throughout your app, as long as each one of them have their own namespace.

[0:39] You define a namespace simply with the namespace keyword, and it needs to be the first line in your PHP class.

[0:47] For example, let's say we wanted this author class to be in the app namespace. We can simply type namespace app, and now this author class belongs to the app namespace. Let's go back to the post class and we will notice something interesting.

[1:06] The author class can actually not be found now. It's undefined according to the PHP code. This is because this class is no longer in the global namespace, but rather in the app namespace. We can get things working again by actually prefixing this class with app/.

[1:26] Now PHP is able to resolve these two classes. When we save and refresh, we can see that our app still works. This app author is known as the fully qualified class name. You may hear that term when you get deeper in your knowledge about classes in the future.

[1:45] Since we could have many references to our class within this file, it's probably a better idea to import this class at the top of the file. Then we can just reference it with author.

[1:58] We could do this above our class name by typing use, which is essentially an import keyword, followed by the fully qualified class name. This would be app/author. This makes the author class available to this entire class.

[2:17] We no longer need a reference to the namespace when we reference that class. We can also alias this class by adding in an as, and then the alias of the class name. This could be useful if you are importing many classes and they share the same name, which does happen from time to time.

[2:36] We can name this something like author alias, or even just another name. Then we can replace the calls to the class with the alias name.

[2:50] When the alias is used in reference throughout your code, it will refer back to this app author class. For now, we will just import it like a regular class is imported. Let's continue by adding a namespace to this post class. Let's add it to the same namespace as our author.

[3:10] We will again use namespace app. You will notice after typing that line that the import of the app author class is now grayed out. This is because both of these classes are now located in the same app namespace.

[3:25] Since they are at the same root of the same namespace, this import is unnecessary as PHP will be aware of all classes in the same namespace. We can remove this class import and everything will still work. Finally, you will notice this post class is also grayed out.

[3:45] PhpStorm is telling us that this class is not currently being used. If we go back to our index.php file, we will see that this post class is undefined for the same reason, it doesn't know how to locate this class. It looks in the global namespace, but it can't find it.

[4:06] All we need to do is import this class with use app/post, and now this class is resolved. A refresher of our page will confirm everything still works.

~ 18 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today