Playing
01. PSR-4 Autoloading with Composer

Episodes

0%
Your progress
  • Total: 6m
  • Played: 0m
  • Remaining: 6m
Join or sign in to track your progress

Transcript

00:00
Alright, so this is going to be a very quick introduction on how to get set up with PSR 4 autoloading within your project just in case you want to follow anything that uses PSR 4 autoloading. So to start with I just have an empty project folder here and I also have my command line or terminal open here as well.
00:23
It doesn't matter which operating system you're using, this will all work in exactly the same way. So step one is to head over to getcomposer.org and we can go over to the download section just here. So there's a couple of ways to download Composer. We can either use curl or if you don't have curl you can use PHP to download this directly.
00:46
Now you can set Composer as a global dependency which means you can just run the Composer command but if you don't want to do this and you're just messing around you can use the composer.phar file and we'll look at doing that as well. Okay, so I'm going to download this using PHP so remember I'm within the project that I want to work on. I'm going to paste this in and that will go and download Composer for me.
01:12
So you'll notice now that within here I now have a composer.phar file. Now you can upload this to your server if you want so you can run things through the command line but you don't have to. It's entirely up to you. Okay, so now that we have done that let's look at how we can actually use this.
01:31
So to run any commands we use PHP and we say composer.phar and then we give whatever command we want. So it's install or whatever. So in our case we're looking at PSR for autoloading. So the first thing we want to do is create a composer.json file and this will store our project dependencies and all that kind of stuff.
01:54
But what we're focusing on is autoloading. So we create an autoload property. This has an object inside which will have PSR for here and then in here we give the name of our application. This is the vendor namespace.
02:13
And here we have the application directory that we want to load from which is usually just called app or source or something like that. Okay, so now that we've done that we need to generate our autoload files and this will create a vendor folder for us. So let's head over to our terminal. Let's run phpcomposer.phar or if you've installed this globally or you've set it global you can just run composer and then this command.
02:41
So we say dump autoload. We pass the optimize flag and that has now generated our optimized autoload files. So we have this vendor folder that we've created and this has autoload.php in here. So before we do anything let's just create this app directory.
03:01
So anything under this app directory now will have the namespace of app with a capital A. So what we can do is in here we can create a class. So for example we could say config and then inside of here we could create a new file called config.php. And let's go and just create a class here.
03:22
So we give this a namespace. Remember it's under app. It's within the config folder and then we have a class called config. Now if you had something else under here, so for example if you had helpers and then you had another file called time.php,
03:42
the namespace for this would be different. It would be app helpers and then the class name is time. And it doesn't matter how many subfolders you use. You just append this on like so.
04:00
And this will become more clear when you're actually working on a project. Okay, so now that we have our example config.php file we need to test this out. So generally what I do is if I was working on an example project like this I would create some kind of bootstrap file. And what this will allow me to do is require in vendor autoload.php and we could stick dir on there like so.
04:31
And now what I can do is any file I create, so for example if we had an index.php file, all we now need to do is require in app bootstrap.php and now we can create a new config class. So we can either define the namespace here like this and we'll just check that this is working by doing a var dump on config. And if we just pull open our browser you can see that that works perfectly.
05:03
The other option is to go and at the top of whichever file that you're working with, you just say use app config config, config class like that and it works in exactly the same way. So when you go and upload your files to a server, same with any kind of composer functionality, what you don't want to do is copy over your vendor folder.
05:25
You can copy over your composer.json file and once your files are on your server, you can then run the same command that we've been working with on your server and that will go and generate your files and it means you don't have to transfer all of your vendor files over. Now if you can't get SSH access to your server, uploading your vendor folder is fine,
05:49
but really using composer is about being able to port a project around, run a simple command and get everything set up. And that goes the same for composer's install command if you have any dependencies loaded in under require. You may have seen this before. Either way that's how to quickly get set up with PSR for autoloading using composer.
1 episode 6 mins

Overview

A quick guide to downloading and PSR-4 autoloading with Composer.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!