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.
54 episodes4 hrs 45 mins

Overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

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

Comments

No comments, yet. Be the first to leave a comment.