Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "resolved" / "resolving" term actually mean in PHP?

Tags:

oop

php

I'm still learning PHP, and I assume it is not so hard to understand that I'm not a native speaker.

At this moment, after reading a lot of documentations, I jumped into the deep water, so I opened Laravel source files and went file after file, trying understand better how the whole implementation of MVC include routing, Middlewares getting together into a high level piece of code.

I've seen a lot of terms I never knew before, but some were easy to understand, while the term "Resolved" and "Resolving" break a little bit my head.

What does it really mean?

Few examples from the documentations in the files:

  1. "Flush the container of all bindings and resolved instances."

  2. "An array of the types that have been resolved."

  3. "All of the global resolving callbacks."

  4. "All of the after resolving callbacks by class type."

  5. "All of the after resolving callbacks by class type."

Now I'm a bit confused, resolved and resolving in a simple translation to my language means to settle down, to decide, to determine, to separate, to breakdown, but nothing of them making any sense for me at the code.

Can you please clarify a bit about the resolving meaning? as I feel once I will understand this term, all will go more smoothly.

like image 817
MyLibary Avatar asked Feb 03 '16 18:02

MyLibary


1 Answers

so I opened Laravel source files and went file after file, trying understand better how the whole implementation of MVC include routing, Middlewares getting together into a high level piece of code.

There is a better way to understand the framework source. Use the debugger.

Take any simple application example, setup the XDebug and go through the whole process of the request handling.

First jump over the details and find out which classes and methods are involved into the request processing, then you can go deeper and deeper.

At least for me this way works much better than just reading the source. You'll quickly find what is important and what is less important and will be able to concentrate on the essential parts.

Now let's get back to your resolution questions:

"Flush the container of all bindings and resolved instances."

Here is the source file - Illuminate/Container/Container.php. And the related documentation: https://laravel.com/docs/5.0/container.

Actually, to understand the source, you first need to understand the concepts in the documentation. In this case they are talking about the dependency injection container and you need to understand how it works:

  • We define the dependency, for example public function __construct(FooBar $dependency)
  • We do not pass these dependencies directly: we don't do $foo = new FooBar; $object = new MyObject($foo);; instead we configure the Laravel's service container and it resolves these dependencies for us
  • So the resolution here is the process when we ask the service container: "give us FooBar, please" and it goes through it's internal registry and finds out (resolves) what object should actually be created

Why do we do this? Because we don't want to hard code the dependencies.

For example, you can have a class which depends on Mailer. On production you want this Mailer to be SMTPMailer and while testing you want to use MockMailer which writes emails to the file.

So you configure Laravel service container to resolve Mailer as MockMailer locally and to resolve it as SMTPMailer on production.

                                  ---  resolution process ------
                                  |                            |
                                     ---------------------
                                     |                   |         ----------------
--------------------------------     |     Container     |    ---->|  SMTPMailer  |
| MyObject                     |     |                   |   /     ----------------
|                              |     |   | internal |    |  /
| __construct(Mailer $mailer) -|---->|   | registry |-------       ----------------
|                              |     |   |          |    |         |  MockMailer  |
-------------------------------      |                   |         ----------------
                                     ---------------------

In MyObject we define some abstract Mailer dependency which is then resolved by the Container into SMTPMailer or MockMailer, depending on the configuration.

Check more usage examples and explanations in the Laravel docs.

"An array of the types that have been resolved."

This is an array of the types (like FooBar) we requested before, which have been already resolved (we already know what actual class maps to FooBar).

"All of the global resolving callbacks."

This is an array of callbacks which are used to resolve dependencies - this is a part of the Laravel's internal registry.

"All of the after resolving callbacks by class type."

These are callbacks which are called when the resolution complete. Probably this is related to this section in the documentation - container events.

like image 200
Boris Serebrov Avatar answered Nov 15 '22 16:11

Boris Serebrov