This episode is for members only

Sign up to access "Build Your Own PHP Framework" right now.

Get started
Already a member? Sign in to continue
Playing
51. Registering functions with PSR-4

Episodes

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

Transcript

00:00
To clean up our code, we're going to be registering a few helper functions over the next few episodes. But first of all, we want to figure out how we can register these helpers so they're globally available in our application.
00:12
Normally, what we would do within a PHP application that isn't using object oriented programming is we would come over to anywhere we're sort of bootstrapping our app and we'd define a function out like this. Now, we don't really want to do that. We want everything to be nice and tidy. So we're going to leverage Composer to register these functions for us and autoload them in.
00:33
To do this, we're going to head over to Composer JSON and just below where we registered our PSR4 app namespace, we're going to go ahead and give a list of files that we want to register in. So you can add as many here as you want to. But let's go over to our main app directory and register a helpers.php file that we load in. Now, that doesn't exist at the moment.
00:53
So I'm just going to go ahead and create out a PHP file in here called helpers and we can just register any functions in here that we need. Let's just register an example name function that just returns my name and then we'll do something useful in the next few episodes. OK, so we've registered helpers. We've added that to compose.json.
01:12
The one thing that we do need to do is just dump the autoloader. So if we run Composer dump autoload, optionally passing in the optimized flag, they will now be loaded in. So we can add any functions we want now to this helpers file and we'll have them available. OK, let's go ahead and just try this out.
01:29
So let's go over to the home controller and let's just dump this at the top of the page and kill the page here. So I'm just going to invoke the name function here. If we head over to the home page, sure enough, that works really nicely. Let's go over to the next few episodes and actually register out some helpful functions that will clear up our code.

Episode overview

In this episode, we're getting ready to clean up our codebase by organizing some helper functions. Rather than scattering function definitions throughout the app (which can get messy fast), we're making these helpers globally available in a clean, maintainable way.

We start by looking at the usual approach—just declaring functions somewhere in our bootstrap code—but decide we want something tidier. So, we use Composer's ability to autoload files, setting it up in our composer.json file right underneath the PSR-4 namespace registration.

We then create a new helpers.php file in our app, register it in Composer, and add a sample name() function. After dumping Composer's autoloader, these helpers become available anywhere in our application. To make sure everything works, we quickly call our new function from the Home controller and see the expected result.

In the next episodes, we'll get into adding genuinely useful helper functions that will help tidy up our code even more!

Episode discussion

No comments, yet. Be the first!