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
07. Autowiring with reflection

Episodes

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

Transcript

00:00
Auto-wiring is the process of grabbing a dependency out of our container or from somewhere else in our application using PHP reflection. Now that sounds a little bit more complicated than it is, but I'm going to show you an example now of swapping over our container or adding a reflection container.
00:19
So let's take a look at how we do this. Before we do anything, let's take a look at an example of grabbing out some sort of class from our application. So I'm going to go ahead and create an example class in here and we're not going to do anything with this. We're just going to keep this as a plain example object.
00:37
Now, normally what we would want to do to be able to access example and grab this out of our container is add this here. So, for example, what we could do is say example, grab the fully qualified class name to this and then return the concrete implementation of this. And this would usually be something like an interface. Either way, what we can now do is update our services.
01:03
So we know that our container gives us or this service provider gives us this example. And then over in our bootstrap app file, we can say get and we can pass the name of the class in here to grab this out. So all that's going to do now is just return to us an instantiated example class, which is great. So that's one way to do it.
01:25
But if we hadn't added this to our service provider. So, for example, we could just get rid of all of this in here and we could get rid of this from our services. When we try and access this, of course, it's not going to work. So let's just try this now.
01:38
Okay, there we go. So alias app core example is not being managed by the container. Now, as soon as we add auto wiring here, what this is going to do is it's going to find this and it's going to automatically resolve it for us. So how do we set this up?
01:54
Well, it's very easy on our container. We're going to add a reflection container delegate. So this is a new reflection container from that package. And we should now be good.
02:10
Let's go over and give this a refresh. And there we go. So what this means is not just being able to grab any instance of something out of our container, even if it's not registered. What it also means is inside of this, inside a constructor, we can then pull out further dependencies.
02:29
So think of this example class like a controller. So eventually we're going to get to the point where we're going to create a home controller and we want to return a view. Let's imagine that we have a class in here and we're going to get rid of these files a little bit later. And let's say we have a class called view and we want to access this inside of here.
02:48
So let's go ahead and create this out. And in example, we want to pull this out directly in our constructor. Now, normally what we would have to do to be able to have this in our constructor and resolved is pass it into the example class. So we would have to do something like this.
03:05
We would have to not use the container and we would have to say example, new it up manually inside of here. And then in here, new up view, and then that would be accessible within here. Let's just die here and we'll do just a file dump on example, just to see this in action. So over an example, we're going to accept view into here and then we should have an instance of that in there.
03:34
And yeah, that's not found. So let's come over to bootstrap and app and just import that class from app core and view. Let's go over. And there we go.
03:42
So we now have an instance of example with an instance of view inside of that property. Now, now let's get rid of this and let's try and resolve this from the container and see what happens. So let's go over, give that a refresh and we get exactly the same thing. So what auto wiring is done here when we resolve example out of the container, it's detected with PHP reflection that we also need an instance of view.
04:11
So it's gone ahead and created an instance of that and passed it into the constructor without auto wiring. If we go ahead and just disable this here, let's go ahead and refresh this. And you can see example is not being managed by the container. But if we were to new this up manually, of course, we would still then have to pass view in.
04:29
So just having this reflection container added means that we can create dependencies or pull in dependencies into any of the classes that we use without having to manually do this. And when it gets to, like I mentioned earlier, using controllers, if this example class was a controller that needed access to rendering a view, all we would need to do is just define this out as a property inside of our constructor and we will have access to that directly. So auto wiring here is super important and we now have it set up.
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!