Autoloading is one of peach bees most useful features when it comes to object-oriented code Gone are the days of being forced to include a huge list of code on the off chance that later some of your code might? Need it all you need to do is register an autoloader then make sure your autoloader and files use the same rules Let’s take a look at how we might make our own auto loader Let’s take the code from our last lesson and adopt it to use Auto loading Auto loading is important because as our application grows. It seems only logical that the code. We depend on gross – as We develop will most likely have more than just HTTP and Twitter but all sorts of other includes – To save us including each of them manually let’s make our own auto loader allowing other developers or future you to add dependencies easily Let’s tuck this auto loader away in a bootstrap file to keep our index nice and clean you Go to our workspace and add new file We can just create bootstrap or PHP and we’ll just shove it in the root here Then the code that we answer will look like this here. We can see an auto loader being registered with an anonymous function The anonymous function acts as a callback Basically whenever a class is referenced that PHP does not recognize it We’ll hit this callback and pass the name of the class in as a string argument, which we can see here on line 3 Our callback then accepts that string as an argument line 4 will replace any back slashes with a forward slash PHP knows this means a directory separator Regardless of your operating system the reason we have to back slashes in the first argument is the only put in one Backslash in here would escape the single quote and now cause a syntax error so we take one of these slashes out here You’ll see the syntax is all changed and that’s is broken so you need to back slashes here We then create a new string starting with the current directory Appending the source SRC folder and use that new class name and suffix PHP on the end of the string to make a full file path Now we include this file otherwise PHP will know how to use it. Let’s go back to index dot PHP and put this all together So we no longer need to include these files manually so we can delete this line And we can change this include here, so instead of including one of the classes directly We can say that we’d like to include Bootstrap as try having a look at this in our preview mode and see if it worked Perfect even though we did not explicitly include the classes we wanted as we did in the last lesson PHP is still happy because it knows where to find the code Now it’s important to have a base understanding of making your own autoloaders, but it might not always be a great idea or your own for years autoloading code in PHP was a bit hit or miss and this was down to everyone wanting to write their own autoloaders and organizing their code in their own different ways This men implementing 10 different packages could mean ten different styles of autoloading being used These days we have the PSR zero standard and a new and shiny standard called PSR for These two standards were written by the PHP framework interoperability group to make autoloading dependencies incredibly easy We’ll look more at how psr-4 works with composer in the upcoming lessons

Autoloading – PHP Standard and Best Practices Part 10

Leave a Reply

Your email address will not be published. Required fields are marked *