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
08. Refactoring to a singleton

Episodes

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

Transcript

00:00
A little bit later on in the course, we're going to want to create some helper methods that are going to make our code a lot more fluent. Now, at the moment, over in Bootstrap and App, our container exists just as a variable inside of here.
00:12
So we can't access our container directly from anywhere in our application. We can do this inside of Bootstrap and App and set everything up, but we cannot just grab an instance out wherever we need. Now, we're not going to see too much of an issue right now, but later on, like I
00:28
said, when we create these helper methods where we need to access stuff directly out of our container, this is going to be super useful. So what we're going to do is change over the way that we get our container to a singleton.
00:41
That means that we can access the instance of our container anywhere in our app at runtime and just grab out what we need. And we'll come back to this a little bit later when we start to create these helper methods just to recap.
00:53
Okay, so for now, I'm going to leave this example and view class in here just so we can experiment with them, but let's change out the way that we knew up our container and move it over to a singleton. So we're going to create a class out in core called container, and this is where
01:09
we're going to access our container from. Now, to create out a singleton within PHP, we first of all create out a static instance of whatever we want to resolve out at the top here. Next, we go ahead and we create out a method like get instance, and that's going
01:25
to return to us either the already resolved instance or it's going to new it out for us. Don't worry if this doesn't make too much sense right now. We'll see how this works in a second.
01:35
Okay, so we're going to go ahead and say if the instance that we've got within this property in here is null, so we're going to access this statically as that instance. So if it is null, we want to new it up. Otherwise, we want to go ahead and return, and even after it's resolved, the instance
01:53
that we have. So what is this instance going to be inside of here? Well, it's going to be exactly what we did over here, newing up a container. So we're going to go ahead and set that instance to a new container from league
02:10
container, not from app core. So obviously, we have the same name here, so we have to just pull this in inline. So now what's going to happen is whenever we invoke get instance, if it's not created already, it's going to create it and return it to us.
02:25
Otherwise, if it is created, it's just going to return it to us, and that's the singleton pattern in PHP. We wouldn't use this too often, but for something like this, it works really nicely.
02:35
Okay, so now instead of new container, we want to use the container that we've just created to grab that instance. So let's get rid of the namespace, and let's go ahead and statically call get instance, and we shouldn't see any difference at all.
02:52
So if we head over and give this a refresh, you can see that nothing looks like it's happening, although we're trying to pull this out of our container. The reason that this is happening, if we just go ahead and remove our error reporting, is we're calling a non-static method get instance.
03:06
So just an error there in the way that we defined that. So let's make sure this is a static function called get instance. Okay, now that we're doing this, let's go ahead and disable error reporting, give that a refresh, and everything is working as normal.
03:20
Okay, let's look at an example of now why this is useful. Now, anywhere in our application now, rather than referencing this container variable, what we can do instead is say container, get instance, and then we can go ahead and just resolve out anything we need.
03:35
So we don't want to do that inside of here. We then want to say get, and then pass in anything we want to resolve. We can do this anywhere in our application now, even if we don't have access to this container variable, and this will grab the current instance of the container, and then
03:50
we can get anything else out that we need. And you can see that that works in exactly the same way. So we'll see this a little bit later on in the course, as we add more stuff to our application, and we need to use this.
04:02
But for now, we've changed it over, and it's ready to go. Okay, before we go, let's go and just get rid of example and view, because we're not going to need these anymore, and we'll go ahead and get rid of this from here and return back to our app run.
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!

Episode discussion

No comments, yet. Be the first!