Check out a free preview of the full PHP Basics course
The "Autoloading Classes" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:
Maximiliano introduces spl_autoload_register, which registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined.
Transcript from the "Autoloading Classes" Lesson
[00:00:00]
>> Maximiliano: By the way, will it work?
>> Maximiliano: What your brain compiler says. I mean, we may have type or something, I don't know, but there is one missing step. It's not gonna work.
>> Student: Including the object.
>> Maximiliano: Including DV. So you cannot do this. I cannot just go and use the class automatically.
[00:00:28]
So we know how to solve the problem, right? We've seen how to solve the problem. Maybe, but I wanna show you, actually, I will do it at the end, typically, but we can do it now. There is another way. So instead of including the class manually or including a file that will include all the classes, we can do something like this.
[00:00:52]
I can include a class.php or classes.php, okay? I'm going to create a new file with the name classes.php, okay? I will show you some of the advanced ideas that we have these days in PHP. So in this, I want to create that file so classes, save this as classes.php.
[00:01:21]
HP, no. Hewlett-Packard, no. Php. There we are. So, php, I'll need to close it. So you know that I can go and include classes DB.php, okay? But I don't wanna do that, because later in my project I will have a lot of classes. I don't wanna remember every time I create a class, I don't wanna remember to go and add one include.
[00:01:51]
So I'm going to use something known as Auto Load. That sounds good. So what's the idea? So there is a function in php that it's called spl_autoload_register. Let's go and try to find first the documentation for it. There we are. Spl auto register. It will register the given function as an auto load implementation and say, Okay, what's an order load?
[00:02:26]
Okay, so it's a callable by the way. When you see a type with the question mark at the beginning as a prefix, it means it's optional, so it means it might not be there, okay? So here it's explaining what it's doing, blah, blah, blah. So what's the deal with this spl_autoload_register?
[00:02:49]
We're going to register a function and something that we haven't seen yet, so it's a good opportunity to see that we can create also lambda expressions. And that means that we can pass anonymous functions in PHP as in JavaScript. So instead of creating a function outside and just pass the function as an argument as in JavaScript, I can create a function on the fly in PHP.
[00:03:16]
In fact, it's if I don't tell you, this is a PHP file, you may think this is JavaScript, okay? Also we have arrow functions if you want, okay? So we also have arrow functions that look pretty similar to that, but it's typically common for expressions that fits in one line.
[00:03:38]
So this function will receive a class name as an argument. Every time you call new, with the name of a class, if that class does not exist in the current context, it will execute this function saying, hey, hey, I'm trying to create an object of the AAAA. Do you know what to do?
[00:04:03]
Because I don't. Does it make sense? So, and then if we are following the pattern of naming the classes with the same name as the file, we can match that situation and we can call include or include ones. In this case of what? Of classes/.class_name.
>> Maximiliano: Make sense?
[00:04:37]
Anything else? Do we need to add anything else here to make it work? In the class name, for example, if I say new DB, class name will be DB. So it will try to load classes /DB. I think there is one more thing missing.
>> Student: .php?
>> Maximiliano: .php.
[00:04:59]
Will it work like that? Well, we can try.
>> Maximiliano: If you don't like how that looks like, if you wanna be more like this, you can remember use.as well to concatenate strings. In that case, you can use single quotes. I think if you wanna see that in a better way, in a cleaner way.
[00:05:28]
So before, a few years ago, this was done with a different way that was called a magic function. And there are a lot of magic functions in PHP, these functions that are being executed. Okay, if I don't know what to do, let's see if the magic function can help something like that, okay?
[00:05:48]
For example, there is a magic function for classes. So if you are accessing a property, for example, I create DB here and I am accessing a property that does not exist, because I don't have an A property, it can execute the magic function. If I set the magic function, then we receive, hey, someone is trying to set the value of an A property.
[00:06:12]
You wanna do something with that? And then you can create the function and you receive the property name and the value and you do whatever you want. Okay, so that's why it's a really flexible language, PHP, okay? Well, if that works, that means that because this file, indexphp, is including classes PHP, when I'm creating a new database, it should go through that function and it should include my file.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops