Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><em>Where should I instantiate my injected dependencies, such as $database, $session, etc? Would it be outside the container class, or inside the container's constructor.</em></p> </blockquote> <p>Ideally your database connection and session would be bootstrapped in. Proper DI requires an instance of a base object for which everything is registered into. So taking your IOC class as an example you need to make an instance of it (<code>$ioc = new IOC();</code>) then you need some kind of service provider class say</p> <pre><code>$ioc-&gt;register('database', new DatabaseServiceProvider($host, $user, $pass)) </code></pre> <p>Now every time you want a connection to the database you just need to pass in <code>$ioc-&gt;get('database');</code> a very rough example but I think you can see the idea is basically to store everything inside a registry and nothing is statically binded meaning you can create another instance of <code>$ioc</code> with totally different settings making it easy to create connections to say a different database for testing purposes.</p> <blockquote> <p><em>What if I need to create a multiple instances of the User class inside other classes? I can't inject the previously instantiated $user object because that instance is already being used. However, creating the multiple User instances inside of another class would violate the rules of DI.</em></p> </blockquote> <p>This is a common issue and there are multiple different solutions. Firstly your DI should show the difference between logged in user and just a user. You would probably want to register your logged in user but not just any user. make your user class just normal and use</p> <pre><code>$ioc-&gt;register('login-user', User::fetch($ioc-&gt;get('database'), $user_id)); </code></pre> <p>so now <code>$ioc-&gt;get('login-user')</code> returns your logged in user. You can then use <code>User-&gt;fetchAll($ioc-&gt;get('database'));</code> to get all your users.</p> <blockquote> <p><em>I'm wondering if I should even adopt DI, knowing that I have to rewrite all of my previous code. I've previously been relying on global variables that I instantiate in my initialize.php, which is included in all my files.</em></p> </blockquote> <p>If you need to rewrite all your code to use DI you shouldn't probably do it. Maybe look into making a new project and work in some of your old code if you have the time. If your codebase is large I would recommend looking into breaking it down into smaller projects and using RESTFUL apis for getting and saving data. Good examples of writing APIs would be for putting your forum search into its own application /search/name?partial-name=bob would return all users with the word bob in it. you could build it up and make it better over time and use it in your main forum</p> <p>I hope you understand my answers but if you need any more info let me know.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload