1 00:00:00,830 --> 00:00:05,290 The project we're building, we use many different classes and interfaces. 2 00:00:05,290 --> 00:00:08,180 All of which need to be included before they can be used. 3 00:00:09,210 --> 00:00:12,550 Instead of adding each file individually, we're going to make 4 00:00:12,550 --> 00:00:17,130 use of another great component of object oriented programming, autoloading. 5 00:00:18,260 --> 00:00:22,120 Autoloading is triggered when a class is not found. 6 00:00:22,120 --> 00:00:26,700 Before the system throws an error, we can specify a function which will look for 7 00:00:26,700 --> 00:00:30,300 the class, and automatically include that file when needed. 8 00:00:31,530 --> 00:00:34,600 Let's launch the workspace attached to this video and 9 00:00:34,600 --> 00:00:37,320 start by creating our first class. 10 00:00:37,320 --> 00:00:42,110 We're going to put all our classes and interfaces in our source directory. 11 00:00:42,110 --> 00:00:44,590 Let's create a new folder named classes. 12 00:00:49,880 --> 00:00:54,970 We'll then create a new file, Named collection.php. 13 00:00:58,892 --> 00:01:01,832 For now, we're just going to start with an empty class. 14 00:01:08,156 --> 00:01:12,662 In the index.php file, let's attempt to instantiate this new class. 15 00:01:18,336 --> 00:01:19,871 New Collection. 16 00:01:21,919 --> 00:01:24,931 Now, we can preview the site in a browser. 17 00:01:27,223 --> 00:01:31,950 We see an error telling us that the Class Collection cannot be found. 18 00:01:31,950 --> 00:01:36,970 If we go back to WorkSpaces, we could require this new class file first. 19 00:01:49,200 --> 00:01:53,460 This would require us to include each class file individually. 20 00:01:53,460 --> 00:01:55,500 So let's set up an autoloader instead. 21 00:01:56,570 --> 00:02:00,510 First, we're going to create a config file within the source directory. 22 00:02:02,980 --> 00:02:04,030 We'll use this for 23 00:02:04,030 --> 00:02:08,120 all the shared functionality we'll need throughout our application. 24 00:02:08,120 --> 00:02:10,715 We can then include this file on any page. 25 00:02:25,792 --> 00:02:28,700 We're ready to create an autoload function. 26 00:02:28,700 --> 00:02:29,990 Let's take a look at the documentation. 27 00:02:34,860 --> 00:02:36,370 Php autoload. 28 00:02:40,800 --> 00:02:44,045 The first result tells us that it's preferred to use 29 00:02:44,045 --> 00:02:47,240 the spl_autoload_register function. 30 00:02:47,240 --> 00:02:47,960 So let's go there. 31 00:02:49,080 --> 00:02:51,315 Instead of including a class specifically, 32 00:02:51,315 --> 00:02:56,740 spl_autoload_register will trigger a function when a class is not available. 33 00:02:57,790 --> 00:03:01,450 These triggers are sometimes referred to as listeners, 34 00:03:01,450 --> 00:03:05,940 because they passively listen for some specific event to happen. 35 00:03:05,940 --> 00:03:09,360 And when it does, they'll trigger a reaction. 36 00:03:09,360 --> 00:03:14,130 For a trigger to work, we need to register this functionality. 37 00:03:14,130 --> 00:03:18,680 We used a function spl_autoload_register to tell PHP 38 00:03:18,680 --> 00:03:22,870 which function to use when a class or an interface is not found. 39 00:03:23,890 --> 00:03:25,313 Let's scroll to the first example. 40 00:03:28,794 --> 00:03:34,544 We build our function, then we register that function with spl_autoload_register. 41 00:03:34,544 --> 00:03:38,815 php will automatically pass the name of the class that it is attempting to 42 00:03:38,815 --> 00:03:40,190 instantiate. 43 00:03:40,190 --> 00:03:44,280 Unlike the example here, I want to be able to organize my classes and 44 00:03:44,280 --> 00:03:46,870 interfaces into subdirectories. 45 00:03:46,870 --> 00:03:50,025 To do this, let's use another built in function named glob. 46 00:03:54,300 --> 00:03:57,397 Glob finds the pathnames matching a pattern. 47 00:03:57,397 --> 00:04:00,853 We can use the GLOB_ONLYDIR to return only 48 00:04:00,853 --> 00:04:05,360 directory entities which match the pattern. 49 00:04:05,360 --> 00:04:06,525 Let's go back to WorkSpaces. 50 00:04:12,916 --> 00:04:19,723 We'll create our new function, autoloader, and we'll accept the class name. 51 00:04:26,833 --> 00:04:31,483 Then we'll use a foreach loop, With our glob, 52 00:04:33,731 --> 00:04:41,250 The current directory, And all entries. 53 00:04:43,330 --> 00:04:50,564 We use the GLOB_ONLYDIR flag, and then as $dir. 54 00:04:57,061 --> 00:05:01,498 Now, we can check if (file_exists, 55 00:05:04,473 --> 00:05:12,052 Our "$dir/", The $class_name, '.php'. 56 00:05:16,628 --> 00:05:23,559 Then we can require_once, The "$dir/", 57 00:05:23,559 --> 00:05:28,598 $class_name, '.php'. 58 00:05:30,589 --> 00:05:36,520 Then we can use the break; to stop the foreach loop after a file has been found. 59 00:05:36,520 --> 00:05:41,657 Next, we need to register this function, 60 00:05:41,657 --> 00:05:47,235 spl_autoload_register('autoloader. 61 00:05:50,530 --> 00:05:55,924 Now, when a class is found before an error is thrown, our autoload function will be 62 00:05:55,924 --> 00:06:02,170 triggered, which automatically adds the class for us using the require statement. 63 00:06:02,170 --> 00:06:03,128 Lets go back to our preview. 64 00:06:06,255 --> 00:06:10,644 We no longer get an error because the class has been included with our 65 00:06:10,644 --> 00:06:11,600 autoloader. 66 00:06:11,600 --> 00:06:13,980 Now, that we have an autoloader setup, 67 00:06:13,980 --> 00:06:18,020 we no longer have to worry about including any of our future classes or 68 00:06:18,020 --> 00:06:22,860 interfaces, as long as they're stored in a directory, below the source directory.