Namespaces & Autoloading | PHP for Beginners - Part 22

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay guys let's move on and let's talk about namespaces in autoloading okay what is namespace in general in PHP imagine the following example that you are creating an application in your application you have the following classes you have email class you have person class and you have order class and you want to use email sending package which is a third party package and that package also has email class and you to install that package in your project and use it for email sending you simply cannot do this because you have a name collision of classes you have email class declared and the third party package also has email class declared and that is that was big problem a years ago but namespaces can solve this problem what you simply need to do is that create an app namespace and put all your classes inside app namespace okay so you create you move your email class into app namespace and its identifier now is not email just email its identifier the class is identifier is up peg slash email and the same thing happens for person and order okay your class name is now up back slash email up back slash person and up X slash order and now you can easily install third parties email class in your in your application because you won't have name collision one is called email second one is called up email okay I hope this makes sense so we need to do now the same thing and let's create namespaces in the PHP store okay so here I have opened index.php from autoloading folder and on the right side i have opened it autoloading folder and now let's create email in person classes okay so I'm going to create right here email class and let's remove this and I'm going to give it a name space now okay so this is this email class now is it cleared in the global namespace so I'm gonna give it a namespace up so this is the way how we can give namespace ok namespace up let's create another class call it person and the phpstorm detected that we had already emailed class with app namespace and it created the person class inside the app namespace also okay so let's let's do like this so I'm going to create also app folder and move both of the classes right there okay so in the index.php now i want to use the eight classes so first of all i need to use time to call require once to include that class code and then i can use an instances i can create instances from this class okay for example email equals new email and here is the thing so that is the classes fully qualified name okay creating an instance just like this won't work and if we if we just try to see the result in the browser that is an error and code error class email not found okay that's because now the class name is up email and whenever we want to use that class we need to use with up email okay so that's that's the rule what you need to remember and here that first forward slash is kind of optional okay so again this topic are is explained really well in my object-oriented PHP playlist so definitely check out the playlist there are some scenarios about namespaces and i have like a full 30 mins video i think about namespaces and definitely check out and you can find the link as always in the video description okay so this is the one way how we can create an instance of the email class and let's by the way print something in the constructor to make sure that is creating an instance okay so eco email class okay let's put beer and email class so it's morning index.php and i'm going to show you a second way how we can use that email class okay so if I have to create an instances of email class multiple times probably I need to use the user operator okay so I say use up email class in this file and whenever I need to create an instance of the email I just can use email because I have used it inside the class and now I can access it with that email name okay so I'm gonna save this refresh the browser and AC email class three times the same thing basically happens about the person okay so the person is also declared under namespace up and whenever I need to use I need to use with either use operator use up person or when I create an instance I need to use equals new up back slash person either one of these either this way or that way okay so now imagine this scenario that your application has multiple classes not just two but 10 20 maybe hundred if your application gets large so whenever you need to use one particular class the email class for example in like a twenty different files you have to always require make sure that the email cousin is included you have to write that require once and then you start using it okay that's why composer was created composer is a tool which gives you which solves you two main problems one is autoloading so all these classes are will be auto loaded whenever you start using it and the second advantage is that it's also package management tool so you can easily install someone else's third-party packages in your project okay let's go ahead and let's install composer okay so here is my new installed windows and I'm gonna type their composer and go to the download you can click on the composers main page and then go to the download and in download section you will see that composer setup exit ok so let's click that it will download that composer setup accept and before we install let me show you that it's not installed and how how it actually works ok so we need to open CMD and type their composer okay so composer is not recognized as an internal or external comment ok because we don't have it installed now let's start installing that package ok we just need to follow the instructions nothing special so it's automatically detected where the PHP was installed that's important so composer runs with PHP so it needs to know where the PHP executable file is located ok so we just follow this just next and here it is it detected the PHP version in the excel file and let's install ok so it it was actually installed and let's open CMD once again and type their composer and here we go it gave us some kind of output and these are the comments that the composers of course of course I'm not going to cover all of them we're gonna simply see couple of them in our project ok so let's get back to our PHP store and use that composer comment which we have already installed okay so since we have successfully installed composer we need to navigate to the project folder and create a composer JSON right here ok so here is my autoloading folder and i'm gonna create composer JSON right here i will explain what is composer JSON in seconds okay so let's open that place so I'm going to copy that file path and navigate right there okay so we're in outer loading area so I'm gonna run right here composer in it that will ask me a couple of questions what's the package name I'm gonna hit enter on all of this what's the package description what with the health or minimum stability and some other stuff and I'm gonna hit enter on all of these and that will give me composer JSON right here that composer JSON is the configuration file for your application for your project okay so we give some whenever we create composer Jason we need to give some unique name of our package if we are going to publish that package and make it available for others okay so here's the my name which I actually are not going to publish it here are some other information but that also composer will also give a possibility to enable auto loading okay so here we need to write outer load down to load equals P is r4 which is an object and inside there we're going to write the namespace name and on which folder that namespace is mapped so I will write here namespace will be up and this will be mapped into the art folder okay so under app folder all of the classes will be under app namespace okay let's save this and go to the terminal we need the terminal and we need to run composer update okay so that we'll need to update some outer loading files okay yeah I missed one important thing in the hub we need to put their big slashes okay so we actually need to put here brakes less but as far as single backslash came but they are we need two backslashes okay so that's like this and let's say this and run composer update okay generating auto load of files that's cool let's have a look so it created that a vendor folder and that vendor folder contains the auto load PHP in that file is basically responsible to load classes whenever we want to use this okay now if we go to the next PHP and comment that require ones lines and save that file and refresh the browser we still see that up email cannot be found and that happens because we need to simply require just one file in that one file will be render out to load PHP okay so that outer world PHP is responsible to include all other classes what we're going to use even if they there are hundreds or thousands of classes that auto load PHP will handle all of them and require but we need to require the total PHP and let's save this and see the result in the browser and here we go we don't need that require once anymore we can create as many classes as we want in the app folder and then we can just use them with either use operator like this or we can just write a person just like this okay so that's all about Auto loading what you need to know again check out my full playlist about object-oriented PHP you can find the link in the video description or in the top right corner of the screen and that's all about Auto loading composer is the tool which is used in every modern PHP framework or CMS so because as I said it fixes two main problems one is outer loading they tweak with have to write require or require 100 times if we have hundred crowns in our application and second one is that it also fixes the dependency management problem okay which we haven't mentioned yet let's imagine the case that's now we want to install the third party package in our Auto loading folder okay there should be a way for this let's go to the package east dot org which is the file where PHP packages are registered let's go to the Browse and here we see like a couple of packages you will see new packages and there will be some popular ones for example monologue I want to install that monologue that's basically package which is which handles the ascending log log text into like files or sockets or in any other places okay so and here is the installation installation guide so we need to install it with composer okay so we have seen one comment one comment of the composer which is called composer in it and that created that composer JSON now when to run composer require monologue monologue okay and let's open CMD here it is and let's run composer okay let's copy this and run here composer require monologue monologue that will install that will download and install that package in the project in the vendor folder actually and it will also handle all kind of dependencies that that particular package has okay so you see it's installed a two packages although we have requested only one so we requested monologue but that monologue has actually dependency on PS our log okay and it installed that second each also and if we have a look in the vendor folder we see that as well as the PS okay and if we want right now to use that monologue we can already use because we have once included that auto load PHP and we can easily use that monologue and as you see they are these classes are written also in whites in in namespaces okay so we can simply take that code and understand what's going on there okay let me comment this code and down below Pastis okay so we are using monologue clover and some stream handler classes and this will obviously write in the log file okay so let's create like a outputs dot log file and okay so we are creating basically some instance and actually we don't care what's going on right here that will write the output in the log file okay so I have installed package and I can use it and let's access that file in the browser okay something happened and here we go so it created that output log and inside there we see some nice formatting warning and error locks okay here it is we were we have written warning in error and they came right here okay so that's just demonstration how we can install and use someone else's packages using composer and composer as I said is absolutely mandatory tool if you are going to like start working on PHP if you're going to learn like frameworks you have to use composer and it's not like a complex tool you just need to know a couple of comments and whenever you need the others you can simply start googling it okay so that's it and thank you
Info
Channel: The Codeholic
Views: 6,947
Rating: undefined out of 5
Keywords: php, php tutorials, php tutorial, php course, php tutorial for absolute beginners, php tutorial for beginners, php tutorials for beginners, php tutorials 2020, php 7.4, learn php, php 2020 tutorial, php full course, TheCodeholic, php for beginners, php programming tutorial, php programming, php basics, autoloading, php autoloading, composer, php autoloading tutorials, composer install, composer require, composer commands, autoloading in php, PHP composer, Part 21, part 21
Id: NLXtGZ6Ilsw
Channel Id: undefined
Length: 17min 27sec (1047 seconds)
Published: Fri Apr 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.