This is a piece of project code. $this[‘config’] has a ECMAScript assigned to it, but actually new has a new object. What is the difference between writing this way and writing a new object directly? Or what are the advantages?
Class in screenshot
Cashier
It is a Service Container. In short, if you directlynew Config($config)
, then instantiateCashier
When, will inevitably createConfig
Examples, whether used or not. Using closures only gives access dependencies (i.e.Config
Instance), so that instantiation will only be called when needed.For the practice of using closures to handle dependency injection, please refer to this article:http://fabien.potencier.org/o ….