Autoloading6:23 with Alena Holligan
Instead of including each class file individually, we are going to make use of another great component of Object-Oriented Programming: autoloading. Autoloading is triggered when a class is not found. Before the system throws an error, we can specify a function which will look for the class and automatically include that file when needed.
The project we're building, we use many different classes and interfaces. 0:00 All of which need to be included before they can be used. 0:05 Instead of adding each file individually, we're going to make 0:09 use of another great component of object oriented programming, autoloading. 0:12 Autoloading is triggered when a class is not found. 0:18 Before the system throws an error, we can specify a function which will look for 0:22 the class, and automatically include that file when needed. 0:26 Let's launch the workspace attached to this video and 0:31 start by creating our first class. 0:34 We're going to put all our classes and interfaces in our source directory. 0:37 Let's create a new folder named classes. 0:42 We'll then create a new file, Named collection.php. 0:49 For now, we're just going to start with an empty class. 0:58 In the index.php file, let's attempt to instantiate this new class. 1:08 New Collection. 1:18 Now, we can preview the site in a browser. 1:21 We see an error telling us that the Class Collection cannot be found. 1:27 If we go back to WorkSpaces, we could require this new class file first. 1:31 This would require us to include each class file individually. 1:49 So let's set up an autoloader instead. 1:53 First, we're going to create a config file within the source directory. 1:56 We'll use this for 2:02 all the shared functionality we'll need throughout our application. 2:04 We can then include this file on any page. 2:08 We're ready to create an autoload function. 2:25 Let's take a look at the documentation. 2:28 Php autoload. 2:34 The first result tells us that it's preferred to use 2:40 the spl_autoload_register function. 2:44 So let's go there. 2:47 Instead of including a class specifically, 2:49 spl_autoload_register will trigger a function when a class is not available. 2:51 These triggers are sometimes referred to as listeners, 2:57 because they passively listen for some specific event to happen. 3:01 And when it does, they'll trigger a reaction. 3:05 For a trigger to work, we need to register this functionality. 3:09 We used a function spl_autoload_register to tell PHP 3:14 which function to use when a class or an interface is not found. 3:18 Let's scroll to the first example. 3:23 We build our function, then we register that function with spl_autoload_register. 3:28 php will automatically pass the name of the class that it is attempting to 3:34 instantiate. 3:38 Unlike the example here, I want to be able to organize my classes and 3:40 interfaces into subdirectories. 3:44 To do this, let's use another built in function named glob. 3:46 Glob finds the pathnames matching a pattern. 3:54 We can use the GLOB_ONLYDIR to return only 3:57 directory entities which match the pattern. 4:00 Let's go back to WorkSpaces. 4:05 We'll create our new function, autoloader, and we'll accept the class name. 4:12 Then we'll use a foreach loop, With our glob, 4:26 The current directory, And all entries. 4:33 We use the GLOB_ONLYDIR flag, and then as $dir. 4:43 Now, we can check if (file_exists, 4:57 Our "$dir/", The $class_name, '.php'. 5:04 Then we can require_once, The "$dir/", 5:16 $class_name, '.php'. 5:23 Then we can use the break; to stop the foreach loop after a file has been found. 5:30 Next, we need to register this function, 5:36 spl_autoload_register('autoloader. 5:41 Now, when a class is found before an error is thrown, our autoload function will be 5:50 triggered, which automatically adds the class for us using the require statement. 5:55 Lets go back to our preview. 6:02 We no longer get an error because the class has been included with our 6:06 autoloader. 6:10 Now, that we have an autoloader setup, 6:11 we no longer have to worry about including any of our future classes or 6:13 interfaces, as long as they're stored in a directory, below the source directory. 6:18
You need to sign up for Treehouse in order to download course files.Sign up