How to Autoload PHP Files & Classes (PSR-4) - PHP Composer Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys hey going so today I'll be showing you how to auto load your PHP files or classes using composer so this is definitely the recommended way or at least one of the recommended ways to auto load your PHP files so if you don't have composer I recommend downloading and installing it at get composer org and that's going to be a requirement in order for this to work but anyway I'm assuming most of you have composer already so let's go back inside this tab right here then go inside the text editor to order load some files so it's very easy to do I'm gonna firstly just create a new composer type JSON file right here and within this composer JSON file I'm gonna simply put one property here and that is gonna be auto load okay we're gonna keep this empty for now but we're gonna revisit this file in a few moments but aside from that let's leave this and then go inside here now and just create a file structure so let's go and make a new directory here and call this directory source SRC but inside here let's make some classes so firstly I'm gonna make a new directory record data and this will hold all of the data related classes for the application and make a second one here if I can and this one's gonna be let's just say for example a game okay so I'm not too sure where that was I might just I might just remove this folder here sorry and try again I'll make a new file inside data so let's just say database dot PHP and then try making game again if I if I can I'm not sure what this feature is but now there we go so I've got down on our game so inside game I'll make a new games up here to P and I'll make something like a games a game controller for Z or something like that okay and inside game I'll make one called player and inside player we can just say for example player dot PHP so right here this is a directory and file structure of the application so far so we're going to be automatically Auto loading all of these files on each page load okay so we do need to put a few classes and namespaces inside here so let's just say for the database here I'm gonna say class PHP okay so I'm gonna say I'm gonna say class and I'm gonna say database and this namespace is gonna be namespace I'm gonna say for example my app and then data okay inside here we can just say public function construct and for this we can just say echo made database okay cool now I'll just copy and paste this for the remaining files so game can be game that's give me that's gonna be the namespace of my app and then game and then that's all do the same for game controller very similar except game controller I'll also update these echoes asleep many game controller and made a game and for the last one here for player it's going to be my up and in game and then at player and this will be player and mate a player so that is our directory structure now keep in mind that the namespaces here are important for this to work correctly you should be namespace in your classes anyway but this this needs namespaces in order to work correctly so make sure you have your namespaces set in each one of your classes okay anyway let's go and just remove or just close all of these classes here and then go inside the composer that JSON file and inside this autoload property we're gonna add a new property and that's gonna be PSR - 4 just like that and inside here we're gonna specify and we're gonna say my app and then two backslashes then we're gonna say right here SRC so basically we are saying that we want to order load and we're saying that this namespace here can be found within this directory right here so of course all of our namespaces start with my app so it's basically gonna recursively go through each one of these files and it's gonna make sure that they're available when we include the auto load fund that is basically it's in order for this to work and we do need to though we need to actually generate the auto load file so let's go here now inside the terminal and we're gonna say composer dump - auto load then we're gonna say - oh and this right here is gonna generate the auto loader file press enter and we can see in a few moments the vendor directory here contains your auto load dot PHP and now you just need to include this file as usual inside your index or your main PHP file and everything should work smoothly so now let's just close this here and then inside the the source directory we are just gonna say right here I'm gonna say index duck you know actually let's keep it clean let's put it outside of the source directory we can say index dot PHP just like that inside here now we can just simply say require once I'm gonna say real part I'm gonna say a vendor and then auto load dot PHP okay and then we should be able to save this and just make sure everything worked smoothly and we can see no errors so far so let's go back inside here and we're going to try and make one of these classes so let's say for example player equals a new player and BS code is gonna automatically say use and then put the namespace up there of course if I didn't want to do that then I could just say something like this and I could say for example is to make a game controller we can say my app now I can say game and then I can say game controller just like that I can hit save now that's big isn't it undefined function in game control of course new right there I can save this now and then refresh the browser and as we can see about here we get meta player and we get made a game controller so that is how to order so i auto load your classes and files in PHP thanks for watching guys and I'll see you later
Info
Channel: dcode
Views: 27,974
Rating: undefined out of 5
Keywords: code, coding, programming, tutorial, introduction, beginner, walkthrough, guide, software, development, simple, easy, english, with, example, examples, developer, lecture, recording, how, to, web, website, app, application, php, server, side, autoload, auto, load, loading, class, classes, file, files, require, include, once, require_once, include_once, template, blade, laravel, composer, psr4, namespace, namespacing
Id: xWgtKALpx9E
Channel Id: undefined
Length: 6min 41sec (401 seconds)
Published: Sun Jan 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.