- Overview
- Transcript
3.3 Autoloading Through SPL
For very small projects, manually including files and classes is manageable. But once you start to work on bigger projects, things can get very messy very quickly. Spl_register_autoload to the rescue!
1.Welcome1 lesson, 01:07
1.1Welcome01:07
2.The Absolute Basics of OOP5 lessons, 22:53
2.1What Is OOP?04:40
2.2Classes vs Objects05:42
2.3Class Constants and Internal Reference04:19
2.4Public vs Private Scope06:05
2.5Copying vs Cloning Objects02:07
3.Digging a Little Deeper Into OOP5 lessons, 43:52
3.1The Single Responsibility Principle15:18
3.2Magic Methods11:16
3.3Autoloading Through SPL03:37
3.4Working With Namespaces05:14
3.5Autoloading With PSR-008:27
4.OOP inheritance2 lessons, 12:02
4.1Class Inheritance And Protected Scope09:22
4.2Overriding Parent Methods02:40
5.OOP Abstractions5 lessons, 1:00:05
5.1Abstract Classes05:59
5.2Interfaces14:53
5.3Static Properties And Methods06:49
5.4Traits14:44
5.5Dependency Injection17:40
3.3 Autoloading Through SPL
Hello, and welcome back to php oop fundamentals. During this course we've worked on a small number of files. Now with so little files, this is a piece of cake to manage. We can easily include them all inside of our own x.php file. So yes for very small projects, manually including files and classes is manageable. But once you start to work on bigger projects, things can get messy very quickly, and manually including an entire code library, for instance, is basically unmanageable. Luckily, as from PHP 5, we're blessed with auto-learning, and that sure makes learning classes a whole lot easier. An auto-loader is called automatically if your code contains a class reference that isn't declared yet. Now the auto loader searches and loads the appropriate file automatically. Let's see this in action, shall we? Well first at the top of this page, I'll remove all require statements except the one for helper.php because that will contain our auto-loader function. Let's have a look in the browser. Ouch, yeah. That code is broken, alright. Our code is looking for classes in files that have not been included. Okay, so let's get started on that auto-loader now. To do so, we need to open up our helper file. An auto-loader is basically a function that contains a certain set of rules and filters. So let's create one now. We'll just call it auto-load and that needs to be a function and it will accept a single parameter. Let's call that className. Okay, just clean that up. Now I can call this function whatever I want to, but autoload just seems appropriate, okay? First off, we'll need to construct a file name to search for. Now I've conveniently named our files after the classes they contain so all we have to do is add a file extension. Good. Let's create a file variable. And why don't we just include an absolute path. So we'll do File which will give us the current directory. Now that needs to be followed by a slash. Then the class name, of course, followed by another string and that will be .php. So now that we have our file name, we can do a simple check to see if it actually exists. So we'll do if file exists, and all we need to do is drop in the file name there. And then, if it does exist, let's just require that file. Okay, and I think that should take care of things. Now, how can we make sure this function is actually run when we instantiate an object that is not present yet? Well, that's where the actual magic comes in. Just register our function with spl_autoload_register like so, and all we need to do is pass in the function name, and Bob's your uncle. So just to recap, we construct the file name, and I see I forgot two underscores here, directory name followed by a slash followed by classend.php. So if we call a user class the file would be the path to the file and then user.php. Then we check to see if that file exists and if it does, we require it. Let's see this in action. And that all seems to work just fine despite the fact that we're now not requiring our user and validator classes at the top of index .php anymore. So yeah, auto loading can make your life much easier, come to think of it there are even more efficient ways of auto loading using name spaces. So first, let's see what name spaces are exactly, and we'll cover that in the next video, I'll see ya there.







