Demo: Str KpopCase0:43
tricks. But your friends who don't use Laravel may not like this magic. In this short series on Laracasts, we will demystify the Laravel magic so you learn more about how it's done and can share it with your friends. Let's start with the first magic trick. Here I have an artisan command called ApraCadapra. Let's call the dump method inside this command handler. And use the Sutter class or the String class. This one here is Illuminate\Support\String. And then call the kpop method.
Composer Autoloading Explained1:17
This one here is Illuminate\Support\Str. And then call the Kpop method. This method transforms a string to KpopCase. Now let's provide a string to the method, ZainSaeed. And run the command, php artisan ApraCadapra. And here we go, ZainSaeed in KpopCase. A Laravel app ships with Composer, which has an autoloader that reads a full class name from our code and requires the correct file from the file system. So when we ask for Illuminate\Support\Str, it requires the Str.php file located inside the support directory of the Laravel source code.
So when we ask for Illuminate\Support\Str, it requires the string or the Str.php file located inside the support directory of the Laravel source code. Autoloading is not magic nowadays. It's been there for years and all php applications and frameworks use it. Now instead of typing the full class name here, we can alias it. And with that, the autoloader knows that when we refer to a Str class in this php file, we mean the Illuminate\Support\Str class. Let's run the command. And here we go, it's still working. Now let's remove the use statement.
Facade Alias Magic2:35
And here we go, it's still working. Now let's remove the use statement. We won't tell the autoloader what we mean by the Sutter class. And we can see here that the IDE is complaining that it doesn't recognize this class. Let's run the command. And it's still working. How? Magic. Let me show you how the magic is done. Let's define a user variable.
Let me show you how the magic is done. Let's define a $user variable. And assign new App\Models\User to it. Now let's dump the return value of one of the functions of this class. Let's say getRememberTokenName. And we run the command. And here we go. If we alias the User class and run the command, still works. If we remove the use statement and run the command, we get an error. Class User not found.
Using PHP class_alias3:29
If we remove the use statement and run the command, we get an error. Class User not found. Let's do some magic. I'll call the class_alias method here. And provide the class name as the first argument, App\Models\User. And the alias as the second argument, user. Here we are telling php when we ask for a User class, we mean the App\Models\User class. Let's run the command. And it works. That's the magic Laravel does behind the scenes.
And it works. That's the magic Laravel does behind the scenes. It aliases some classes so you can use them in your code without having to provide the full class name. It makes it easier to use these classes in your Blade template files, like when you call the Satter class from within a blade template to run some operation on a string. It also makes it easier while writing code in a symbol code editor where you have to remember the full class name of each class you want to use. An IDE on the other hand is smart enough to figure that out for you. But with the symbol editor you have to memorize the full class name.
Tinker Auto-Aliasing4:41
An IDE on the other hand is smart enough to figure that out for you. But with the symbol editor you have to memorize the full class name. Now Laravel uses the same trick in the php artisan tinker command. Let's remove this aliasing. And run php artisan tinker. Write User::find(1) to get the first user from the database. Notice we haven't provided the full class name, just the name of our model. And we run the code. And we can see that the tinker command has automatically aliased the User class to the App\Models\User class.
And we can see that the php artisan tinker command has automatically aliased the User class to the app model's User class. Makes things easier. And now that you understand this magic trick, it won't be magical anymore. We are simply using a language feature to improve the developer experience. And if you are new to Laravel, that may have confused you in the past. But after this video, I hope this magic isn't confusing anymore. I'll see you in the next video.
