Lessons: 18Length: 2.3 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.4 Working With Namespaces

In PHP, classes and functions must have a unique name to function without problems. If we included a third party library class, which would be named User (just like our own user class), then we would be in trouble. Luckily, namespaces come to the rescue.

3.4 Working With Namespaces

Hi, and welcome back to PHP OOP Fundamentals. Now, I promised you we'd be tackling namespaces in this video, so here we are. In PHP, classes and functions must have a unique name to function without problems. Now, this would be fine if you were to build a small application. But larger projects often use third party libraries. For instance, if we included a third party library class within our project, which would be named user, just like our own user class, then we would be in a heap of trouble. Luckily, it's namespaces to the rescue. Now, for those of you freaking out right now, you have already used namespaces. Because all the code we have created so far exists in what is called the global namespace. But you can create namespaces of your own. Think of a namespace as a module or a package or a folder inside of your application that a certain bit of code lives in. Now, to see what name spacing is, I'll first open up our helper file and remove our autoloader. Don't worry, we'll restore auto loading in a later video. For now, let's just open up index.php again and require our user class and our validator class, like so. Let's just do a quick check to see if that's all working. Yep, no problems there. Okay, and while we're at it, I would like to re-factor the name of the app folder, because i'd like it to start with a capital letter, like so. Now let's pretend we're loading in a third party library. And we'll create a new folder for that. And we'll aptly call that Library, like so. Now as it so happens, our library contains a user class, so let's just create that. And we need some way to identify this class. So, inside, I'll just create a constructor. And I'll just have that dump something to the string. Something boring like hello from third party library user class. Let's just close that out. So now we'll just know exactly when this class is being run. Now, the first thing we're going to do is require our third party user class at the top of our index.php file. First, we need to rename this folder here, then I'll just copy this line, turn this into library and we'll of course include library user. And now, this line should throw an arrow. Why? Well, we've just declared two user classes in the global namespace, and that's not allowed. Let's just check this in the browser, and, yep, here it is. Cannot redeclare class user. So, how can we fix this? Simple, just give the second user class its own namespace. We'll just need to go back to that user class, and now at the very top of the page I'll write namespace. And then, this needs to be followed by the namespace that we actually want this class to live in. Now, I can set this namespace to a single word like Library, or a number of words like Library\Auth Just as long as these statements are separated by a backslash. It's a bit like a follow path really. For now we'll just stick with Library. So now if all's well the error should be gone, cuz think about it. We're loading one user class in the global namespace And we're loading one user class in its own library namespace. And you'll see that when I fire up my browser, we indeed have no more errors. So, now how could we use this user class, you know, the one with the library namespace. Well, that's simple. You instantiate your new user class almost in the same fashion as you would instantiate your own original user class. Ok now first of all, I'm going to clean this up, and we'll just do a var dump of the user we created in the global namespace, right? And then beneath this, we'll just create a new user called Nick. Now like I was, we first used a new key word, and that now needs to be followed by the namespace, followed by a back slash, and then followed by the class name. This is our global user, and this is our library user. Let's just dump him to the screen as well. Now by providing the name space and the back slash I tell PHP to stop looking in the global namespace for a moment. And change its scope to the provided namespace instead. So let's fire up our browser again and let's just check to see what we have. Here's our first user that we created in the global namespace. Here's the string that we dump to the screen from our library user class constructor. And here's our actually user library user object. And as you can see, this is an object of the type user and this an object of the type library/user. So what's the advantage of this? Well, think about if we build a bigger application, we would probably use third party libraries for a number of things. And those libraries would have their own classes. Now, without namespacing, these classes would break our application, if they used the same name as other existing classes. Now, if class libraries live in their own namespace, we don't have that problem. How's that for modular programming? Okay, in the next video, we'll set up autoloading again for our namespace classes. I'll see you there.

Back to the top