WEBVTT NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com 00:00:00.076 --> 00:00:04.036 align:middle I've an idea! 00:00:04.536 --> 00:00:06.686 align:middle Let's master PHP namespaces... 00:00:07.086 --> 00:00:09.746 align:middle and let's do it in under 5 minutes. 00:00:10.516 --> 00:00:11.406 align:middle Sip some coffee... 00:00:11.896 --> 00:00:16.866 align:middle let's go! Meet Foo: a perfectly boring PHP class. 00:00:17.676 --> 00:00:18.426 align:middle Say hi Foo! 00:00:19.506 --> 00:00:20.166 align:middle Hilarious. 00:00:21.466 --> 00:00:25.676 align:middle To instantiate our favorite new class, I'll move over to a different file 00:00:25.676 --> 00:00:30.216 align:middle and say - drumroll - $foo = new Foo(). 00:00:31.526 --> 00:00:36.746 align:middle Tada! We can even call a method on it: $foo->doAwesomeThings(). 00:00:37.556 --> 00:00:38.236 align:middle Will it work? 00:00:38.736 --> 00:00:39.486 align:middle Of course! 00:00:39.486 --> 00:00:42.056 align:middle I can open a terminal and run: 00:00:42.056 --> 00:00:47.666 align:middle php some-other-file.php Right now, Foo doesn't have a namespace! 00:00:48.386 --> 00:00:50.946 align:middle To make Foo more hipster, let's fix that. 00:00:51.496 --> 00:00:56.956 align:middle Above the class, add, how about, namespace Acme\Tools. 00:00:57.786 --> 00:01:03.696 align:middle Usually the namespace of a class matches its directory, but that's not technically required. 00:01:03.696 --> 00:01:05.446 align:middle I just invented this one! 00:01:06.516 --> 00:01:07.486 align:middle Congratulations! 00:01:07.866 --> 00:01:10.226 align:middle Our friend Foo now lives in a namespace. 00:01:10.556 --> 00:01:17.346 align:middle Putting a class in a namespace is a lot like putting a file in a directory. 00:01:18.036 --> 00:01:24.406 align:middle To reference it, use the full, long path to the class: Acme\Tools\Foo - 00:01:25.186 --> 00:01:29.196 align:middle just like you can use the absolute path to reference a file in your filesystem. 00:01:29.966 --> 00:01:33.326 align:middle When we try the script now: It still works! 00:01:33.756 --> 00:01:35.276 align:middle And... that's really! 00:01:35.686 --> 00:01:38.206 align:middle Namespaces are basically a way to... 00:01:38.406 --> 00:01:39.986 align:middle make your class names longer! 00:01:40.646 --> 00:01:41.636 align:middle Add the namespace... 00:01:42.016 --> 00:01:45.886 align:middle then refer to the class using the namespace plus the class name. 00:01:46.436 --> 00:01:46.876 align:middle That's it. 00:01:47.446 --> 00:01:52.946 align:middle But... having these long class names right in the middle of your code is a bummer! 00:01:53.386 --> 00:01:59.156 align:middle To fix that, PHP namespaces have one more special thing: the use statement. 00:01:59.686 --> 00:02:06.466 align:middle At the top of the file, add use Acme\Tools\Foo as SomeFooClass. 00:02:07.626 --> 00:02:09.166 align:middle This creates a... 00:02:09.166 --> 00:02:09.646 align:middle sort of... 00:02:09.646 --> 00:02:10.326 align:middle "shortcut". 00:02:10.936 --> 00:02:17.236 align:middle Anywhere else in this file, we can now just type SomeClassFoo and PHP will know 00:02:17.236 --> 00:02:22.486 align:middle that we're really referring to the long class name: Acme\Tools\Foo. 00:02:23.176 --> 00:02:29.856 align:middle Or... if you leave off the as part, PHP will assume you want this alias to be Foo. 00:02:30.666 --> 00:02:32.486 align:middle That's usually how code looks. 00:02:33.316 --> 00:02:36.296 align:middle So, namespaces make class names longer... 00:02:36.716 --> 00:02:42.406 align:middle and use statements allow us to create shortcuts so we can use the "short" name in our code. 00:02:43.226 --> 00:02:48.306 align:middle In modern PHP code, pretty much all classes you deal with will live in a namespace... 00:02:49.046 --> 00:02:51.476 align:middle except for core PHP classes. 00:02:52.266 --> 00:02:56.436 align:middle Yep, core PHP classes do not live in a namespace... 00:02:57.316 --> 00:03:00.756 align:middle which kinda means that they live at the "root" namespace - 00:03:01.176 --> 00:03:03.306 align:middle like a file at the root of your filesystem. 00:03:04.166 --> 00:03:09.236 align:middle Let's play with the core DateTime object: $dt = new DateTime() 00:03:09.346 --> 00:03:15.876 align:middle and then echo $dt->getTimestamp() with a line break. 00:03:16.726 --> 00:03:20.766 align:middle When we run the script: It works perfectly! 00:03:21.356 --> 00:03:27.506 align:middle But... now move that same code into the doAwsomeThings method inside our friend Foo. 00:03:28.396 --> 00:03:30.356 align:middle Now try the code: Ah! 00:03:30.616 --> 00:03:32.116 align:middle It explodes! 00:03:32.216 --> 00:03:33.556 align:middle And check out that error! 00:03:33.706 --> 00:03:41.306 align:middle Class Acme\Tools\DateTime not found The real class name should just be DateTime. 00:03:41.726 --> 00:03:46.076 align:middle So, why does PHP think it's Acme\Tools\DateTime? 00:03:46.606 --> 00:03:49.326 align:middle Because namespaces work like directories! 00:03:50.006 --> 00:03:52.446 align:middle Foo lives in Acme\Tools. 00:03:53.066 --> 00:03:56.616 align:middle When we just say DateTime, it's the same as looking 00:03:56.616 --> 00:04:00.476 align:middle for a DateTime file inside of an Acme/Tools directory. 00:04:01.146 --> 00:04:03.116 align:middle There are two ways to fix this. 00:04:03.926 --> 00:04:07.136 align:middle The first is to use the "fully qualified" class name. 00:04:07.446 --> 00:04:09.116 align:middle So, \DateTime. 00:04:10.496 --> 00:04:12.986 align:middle Yep... that works just like a filesystem. 00:04:14.056 --> 00:04:17.526 align:middle Or... you can use DateTime... 00:04:17.936 --> 00:04:19.466 align:middle then remove the \ below. 00:04:19.726 --> 00:04:23.396 align:middle That's really the same thing: there's no \ at the beginning 00:04:23.396 --> 00:04:27.236 align:middle of a use statement, but you should pretend there is. 00:04:27.236 --> 00:04:30.216 align:middle This aliases DateTime to \DateTime. 00:04:30.946 --> 00:04:32.876 align:middle And... we're done! 00:04:33.526 --> 00:04:39.146 align:middle Namespaces make your class names longer, use statements allow you to create "shortcuts" 00:04:39.146 --> 00:04:41.666 align:middle so you can use short names in your code 00:04:41.666 --> 00:04:47.576 align:middle and the whole system works exactly like files inside directories. 00:04:47.576 --> 00:04:48.576 align:middle Have fun!