1 00:00:00,025 --> 00:00:03,520 Namespaces are a common idea in many programming languages and 2 00:00:03,520 --> 00:00:05,280 PHP has had them since PHP 5.3. 3 00:00:05,280 --> 00:00:08,160 To some developers, they still feel a little new. 4 00:00:08,160 --> 00:00:09,510 So let's take a look at how they work. 5 00:00:10,730 --> 00:00:14,290 The basic idea of using namespaces is to ensure that two different classes, 6 00:00:14,290 --> 00:00:17,830 functions, or constants with the same name do not cause conflicts. 7 00:00:17,830 --> 00:00:21,010 For example, if you try to define two classes called client, 8 00:00:21,010 --> 00:00:23,050 then the second definition will cause a fatal error, 9 00:00:23,050 --> 00:00:25,900 complaining that you can not redefine that class. 10 00:00:25,900 --> 00:00:28,200 If instead, you put that class into a namespace, 11 00:00:28,200 --> 00:00:30,350 you can refer to both of them differently. 12 00:00:30,350 --> 00:00:31,890 Let's take a look at how that works with some code. 13 00:00:33,200 --> 00:00:35,300 Before we get started, pay no attention to line four, 14 00:00:35,300 --> 00:00:38,310 as I'll explain about display errors later on. 15 00:00:38,310 --> 00:00:41,300 So on line seven, we're including a third-party piece of 16 00:00:41,300 --> 00:00:46,070 code that has been placed in the source folder which handles HTTP interactions. 17 00:00:46,070 --> 00:00:48,680 On line eight, we're including another third-party piece of 18 00:00:48,680 --> 00:00:52,130 code which will help us interact with the Twitter API. 19 00:00:52,130 --> 00:00:56,520 Sadly, the developer of both the HTTP code and the Twitter code has not 20 00:00:56,520 --> 00:01:00,340 used namespaces and instead declared the classes into global namespace. 21 00:01:00,340 --> 00:01:03,490 Now we have these two client classes which looks a little funny and 22 00:01:03,490 --> 00:01:05,170 will throw an error when we try and run the preview. 23 00:01:05,170 --> 00:01:10,710 So you can see here, they've had trouble redeclaring the class client 24 00:01:10,710 --> 00:01:12,840 because there's already a class called client. 25 00:01:12,840 --> 00:01:16,500 If we have a look at the code we can see how to fix this. 26 00:01:16,500 --> 00:01:21,340 We go into the source folder and inside the HTTP folder and look at the Client. 27 00:01:21,340 --> 00:01:23,600 You can see here that there's no namespace declaration. 28 00:01:23,600 --> 00:01:30,060 Now our namespace declaration is simply the word namespace and then the name. 29 00:01:30,060 --> 00:01:36,090 We save that and we do the same thing to Twitter and save that too. 30 00:01:36,090 --> 00:01:38,180 Now when we run this, PHP gives us a different error. 31 00:01:39,270 --> 00:01:41,820 The complaint here is the class client does not exist. 32 00:01:41,820 --> 00:01:43,180 Which is completely true. 33 00:01:43,180 --> 00:01:46,470 Now the class client is not being defined in the global space. 34 00:01:46,470 --> 00:01:48,690 It has to be referenced with the namespace. 35 00:01:48,690 --> 00:01:50,240 Let's get back to our workspace and try that out. 36 00:01:52,120 --> 00:01:55,020 So if we remember the names of our namespaces, which were HTTP, 37 00:01:55,020 --> 00:02:00,320 we type that in and we use a backslash to de-reference it. 38 00:02:00,320 --> 00:02:03,260 Do the same for Twitter, and now we've referenced our two different client 39 00:02:03,260 --> 00:02:07,960 classes correctly with their namespaces so PHP knows where to find that code. 40 00:02:07,960 --> 00:02:08,620 We run this again. 41 00:02:10,430 --> 00:02:14,820 We can see that two objects, HTTPClient and TwitterClient, have been created. 42 00:02:14,820 --> 00:02:17,240 We can further improve this code by using auto-loading, and 43 00:02:17,240 --> 00:02:18,510 that's what we'll do in the next lesson.