Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Namespaces2:18 with Phil Sturgeon
Namespaces help to ensure that two different classes or functions with the same name do not break an application. Sometimes this is basic things, like having two different classes with a class named “Client”, like a Twitter Client and a HTTP Client.
Namespaces are a common idea in many programming languages and 0:00 PHP has had them since PHP 5.3. 0:03 To some developers, they still feel a little new. 0:05 So let's take a look at how they work. 0:08 The basic idea of using namespaces is to ensure that two difference classes, 0:10 functions, or constants with the same name do not cause conflicts. 0:14 For example, if you try to define two classes called client, 0:17 then the second definition will cause a fatal error, 0:21 complaining that you can not redefine that class. 0:23 If instead, you put that class into a namespace, 0:25 you can refer to both of them differently. 0:28 Let's take a look at how that works with some code. 0:30 Before we get started, pay no attention to line four, 0:33 as I'll explain about display errors later on. 0:35 So on line seven, we're including a third-party piece of 0:38 code that has been placed in the source folder which handles HTTP interactions. 0:41 On line eight, we're including another third-party piece of 0:46 code which will help us interact with the Twitter API. 0:48 Sadly, the developer of both the HTTP code and the Twitter code has not 0:52 used namespaces and instead declared the classes into global namespace. 0:56 Now we have these two client classes which looks a little funny and 1:00 will throw an error when we try and run the preview. 1:03 So you can see here, they've had trouble redeclaring the class client 1:05 because there's already a class called client. 1:10 If we have a look at the code we can see how to fix this. 1:12 We go into the source folder and inside the HTTP folder and look at the Client. 1:16 You can see here that there's no namespace declaration. 1:21 Now our namespace declaration is simply the word namespace and then the name. 1:23 We save that and we do the same thing to Twitter and save that too. 1:30 Now when we run this, PHP gives us a different error. 1:36 The complaint here is the class client does not exist. 1:39 Which is completely true. 1:41 Now the class client is not being defined in the global space. 1:43 It has to be referenced with the namespace. 1:46 Let's get back to our workspace and try that out. 1:48 So if we remember the names of our namespaces, which were HTTP, 1:52 we type that in and we use a backslash to de-reference it. 1:55 Do the same for Twitter, and now we've referenced our two different client 2:00 classes correctly with their namespaces so PHP knows where to find that code. 2:03 We run this again. 2:07 We can see that two objects, HTTPClient and TwitterClient, have been created. 2:10 We can further improve this code by using auto-loading, and 2:14 that's what we'll do in the next lesson. 2:17
You need to sign up for Treehouse in order to download course files.Sign up