Autoloading3:03 with Phil Sturgeon
Autoloading is the art of only loading what you actually need. It is done very efficiently in PHP, and when a class is requested you can register an autoloader to then locate and include the class for you. When combined with an autoloading standard such as PSR-0 or PSR-4 this is very simple.
Autoloading is one of [UNKNOWN] most useful features when it comes to 0:00 object-oriented code. 0:03 Gone are the days of being forced to include a huge list of code on 0:04 the off chance that later, some of your code might need it. 0:07 All you need to do is register an autoloader, 0:11 then make sure your autoloader and file wants to use the same rules. 0:13 Let's take a look at how we might make our own autoloader. 0:16 Let's take the code from our last lesson and adopt it to use auto loading. 0:19 Auto loading is important because as our application grows it 0:23 seems only logical that the code we depend on grows too. 0:25 As we develop, we'll most likely have more than just HTTP and 0:29 Twitter but all sorts of other includes too. 0:32 To save us including each of them manually, let's make our own auto 0:35 loader allowing other developers or future you to add dependencies easily. 0:37 Let's tuck this autoloader away in a bootstrap file to keep our index 0:42 nice and clean. 0:45 We go to our workspace, and add new file. 0:47 We can just create bootstrap.php and we'll just shove it in the root here. 0:51 Then the code that we enter will look like this. 0:55 Here we can see an autoloader being registered with an anonymous function. 0:58 The anonymous function acts as a call back. 1:02 Basically whenever a class is referenced the PHP does not recognize it will 1:05 hit this call back and pass the name of the class in as a string argument, 1:09 which we can see here on lane three. 1:13 Our call back then accepts that string as an argument. 1:15 Line four will replace any back slashes with a forward slash. 1:18 PHP knows this means a directory separator regardless of your operating system. 1:21 The reason we have two backslashes in the first argument is that only putting one 1:26 backslash in here would escape the single quote, and that'll cause a syntax error. 1:29 So we take one of these slashes out here, 1:33 you see the syntax is all changed, and that is broken. 1:34 So you need two backslashes here. 1:37 We then create a new string starting with the current directory. 1:40 Appending the source SRC folder, and use that new class name and 1:43 suffix dot PHP on the end of the string to make a full file path. 1:47 Now we include this file, otherwise PHP won't know how to use it. 1:52 Let's go back to index.php and put this all together. 1:55 So, we no longer need to include these files manually, so 1:59 we can delete this line. 2:02 And we can change this include here so instead of including one 2:02 of the classes directly we can say that we'd like to include bootstrap. 2:07 Let's try having a look at this in preview mode and see if it worked. 2:11 Perfect. 2:16 Even though we did not explicitly include the classes we wanted as we did in 2:17 the last lesson, PHP is still happy because it knows where to find the code. 2:20 Now, it's important to have a base understanding of making your 2:24 own autoloaders. 2:27 But it might not always be a great idea to roll your own. 2:28 For years, autoloading code in PHP was a bit hit or miss. 2:31 And this was down to everyone wanting to write their own auto-loaders and 2:35 organizing their code in their own different ways. 2:37 This meant implementing 10 different packages could mean 10 different styles of 2:40 auto-loading being used. 2:44 These days, we have the PSL zero standard and a new and shiny standard called PSL 4. 2:46 These two standards were written by the PHP Framework Interoperability Group, 2:51 to make autoloading dependencies incredibly easy. 2:55 We'll look more at how PSR4 works with Composer in the upcoming lessons. 2:59
You need to sign up for Treehouse in order to download course files.Sign up