1

I am learning the popular MVC and trying to implement it in PHP. I am designing a framework with a pure OOP fashion (though I am not expert in PHP's OOP ability. I only have moderate knowledge about it). An example implementation of this framework as shown in the following figure. enter image description here

In this framework I added a Data Access Layer, DAL, (a class to deal with the connection, executing query and transport to and from the database) to abstract the physical database from the rest of the system to easy change of the data source. If a system is only bind to one database of one particular type, this layer is expected to presented in the system using only one object with one connection to the database. And this object will be a dependency for all the Data Mapper objects (i.e., User mapper, Product Mapper).

I am looking for your comments on where to initiate the DAL object in the system. I can create the object in the front controller (index.php) and transport all the way to Data Mapper objects. But it is an anti-pattern according to Here and Here. Even for the same reason, we cannot initiate the DAL object within the factories (Factories can be separated in multiple classes for handling complexities as per Clean code approach ). I cannot use Singleton as that is also going to create lots of problem according to this. So, in your opinion, what is the best practice and place where I can initiate this object and pass it to Data Mapper objects?

N.B.: I disregard the View Logic here as my concern do not have any relation with Views.

Md Monjur Ul Hasan
  • 1,705
  • 1
  • 13
  • 36
  • 1
    You need to read about "DI Containers". As for answer: in the service layer .. and that's usually beyond the scope of a framework. – tereško Oct 31 '17 at 15:11
  • Thanks @tereško, Could you please tell me more about a DI container logic that will ensure only one instance of the DAL object and pass it to multiple Data Mapper required to server one HTTP(s) requests, without putting it in the global state? – Md Monjur Ul Hasan Oct 31 '17 at 15:14
  • This is a great question in terms of research done and ultimate goal (it's regrettably off-topic, though). Kudos for the great amount of research you did and for trying to *understand* how things work (we need more of that!). @tereško is right, you need to read on Dependency Injection. I personally use [Dice](https://github.com/Level-2/Dice) for that purpose, have a look at it. Good luck! – ishegg Oct 31 '17 at 15:32
  • @ishegg Thanks for your reply. I made some study about the Dice framework. As you are familiar with this, could you please tell me how can we use the Dice to make sure when it is injecting a required objects to multiple other objects within one life cycle, and the injected objects are not multiple instance but it is same instance. How can we do that with Dice. – Md Monjur Ul Hasan Oct 31 '17 at 15:47
  • @MHRasel that's a feature Dice has. Dice calls it "shared dependencies". Take a look at [example 2](https://r.je/dice.html#example2) on the documentation. The example is even based on PDO :). – ishegg Oct 31 '17 at 15:50
  • Thanks @ishegg, But my question is still exists as my concern is to know the internals, how it is done. And I would like to make sure this feature is not using global state. I will dig deep in dice. As you get it, this is more of a research than an implementation. – Md Monjur Ul Hasan Oct 31 '17 at 15:57
  • 1
    @MHRasel You can investigate the internals on GitHub. For example, [here](https://github.com/Level-2/Dice/blob/master/Dice.php#L63) you can see how `create()` checks for an existing instance, and returns it if it's shared. And [here](https://github.com/Level-2/Dice/blob/master/Dice.php#L87) you can see how the shared instances are created. – ishegg Oct 31 '17 at 16:15
  • I personally just use standalone library from symfony: https://symfony.com/doc/master/components/dependency_injection.html ... works for me – tereško Oct 31 '17 at 16:17
  • I use [PHP-DI](http://php-di.org/) for that. – odan Oct 31 '17 at 19:57
  • Thanks @DanielO. I know all of these DI framework, but my concern is the theory working behind it. And pretty much all the framework ended up with Anti-patterns IMO. – Md Monjur Ul Hasan Oct 31 '17 at 20:16

0 Answers0