Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was going to write this a comment, but it grew too long. I am not an expert so I will just give my point of view from what I've learned through few years practicing and here in SO. Feel free to use or question any part of my answer (or none).</p> <p>1.- Outside. What does the container do? The answer should be a single thing. It shouldn't have to be responsible to initialize the classes, connect to the database, handle the session and other things. Each class does one thing only.</p> <pre><code>class ioc { public $db; // Only pass here the things that the class REALLY needs static public function set($var, $val) { return $this-&gt;$var = $val; } static function newDB($user, $pass) { return new PDO('mysql:host=localhost;dbname=test', $user, $pass); } static function newUser($user_id) { return new User($db, $user_id); } static function newLogin($session) { return new Login($this-&gt;db, $session); } } if (ioc::set('db',ioc::newDB($user, $pass))) { $user = ioc::newUser($user_id); $login = ioc::newLogin($session); } </code></pre> <p>2.- You shouldn't do <code>$friend = ioc::newUser($row['user_id']);</code> inside your class. There you are assuming that there's a class called <code>ioc</code> with a method called <code>newUser()</code>, while each class should be able to act on it's own, not based on [possibly] other existing classes. This is called <a href="https://stackoverflow.com/questions/2832017/what-is-the-difference-between-loose-coupling-and-tight-coupling-in-object-orien">tight coupling</a>. Basically, that's why you shouldn't use global variables either. Anything used within a class should be passed to it, not assumed in the global scope. Even if you know it's there and your code works, it makes the class not reusable for other projects and much harder to test. I will not extend myself (PUN?) but put a great video I discovered here in SO so you can dig more: <a href="http://www.youtube.com/watch?v=RlfLCWKxHJ0" rel="nofollow noreferrer">The Clean Code Talks - Don't Look For Things</a>.</p> <p>I'm not sure about how the class <code>User</code> behaves, but this is how I'd do it (not necessary right):</p> <pre><code>// Allow me to change the name to Friends to avoid confusions class Friends { function __construct($db) { $this-&gt;db = $db; } function create_friends_list($user_id) { if (!empty(id)) { // Protect it from injection if your $user_id MIGHT come from a $_POST or whatever $st = $this-&gt;$db-&gt;prepare("SELECT user_id FROM friends WHERE user_id = ?"); $st-&gt;execute(array($user_id)); $AllData = $st-&gt;fetchAll() return $AllData; } else return null; } // Pass the $friend object function get_friend_data($friend) { $FriendData = array ('Name' =&gt; $friend-&gt;get_user_name(), 'Picture' =&gt; $friend-&gt;get_profile_picture()); return $FriendData; } } $User = ioc::newUser($user_id); $Friends = new Friends($db); $AllFriendsIDs = array(); if ($AllFriendsIDs = $Friends-&gt;create_friends_list($User-&gt;get('user_id'))) foreach ($AllFriendsIDs as $Friend) { // OPTION 1. Return the name, id and whatever in an array for the user object passed. $FriendData = $Friends-&gt;get_friend_data(ioc::newUser($Friend['user_id'])); // Do anything you want with $FriendData // OPTION 2. Ditch the get_friend_data and work with it here directly. You're already in a loop. // Create the object (User) Friend. $Friend = ioc::newUser($Friend['user_id']); $Friend-&gt;get_user_name(); $Friend-&gt;get_profile_picture(); } </code></pre> <p>I didn't test it, so it has probably some small bugs.</p> <p>3.- If you are learning while coding, you will have to rewrite MANY things. Try to do somethings right from the beginning so you don't need to rewrite everything, but only the classes/methods and adopting some conventions for all your code. For example, never echo from within the function/method, always return and echo from outside. I'd say that yes, it's worth it. It's bad that you have to loose 1 or 2 hours just rewriting something, but if it has to be done, do it.</p> <p>PS, sorry, I changed your bracket style everywhere.</p> <hr> <p>EDIT Reading other answers, while you shouldn't connect to the database with your ioc object, it should be perfeclty fine create a new object with it. Edited above to see what I mean.</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.
    1. COTy for the help! 1) I have never seen the set method used in the container. I'm assuming it is used to create any instances I need to be injected into the methods inside the container class? Also, shouldn't it be static function set($var, $val) { return $this->$var=$val} ? 2) You said not to use $friend = ioc::newUser($row['user_id']); inside my class, but what if I need it inside my class? So the only way to create a list is to send out an array and iterate through the array on the page? It seems messy not to have this code done inside a class. What if I had multiple lists on one page?
      singulars
    2. CO1) typo, corrected. 2) I am actually using an object inside `get_friend_data($friend)`. The `$friend` passed is an object actually. Pass only what you need, but ofc, if you need it, you can pass an entire object to a class (though it's better to avoid it).. You have to be able to test the `Friends` class *on it's own*, and for this you should be able to do it without having a working real User class. The option 1 in my code is done inside. I don't know what your function `create_friends_list()` returned, so I couldn't properly answer that part, thus leaving both options for you to choose.
      singulars
    3. COWhat is a good name to call my container object? Is $ioc or $reg acceptable? Also, are the methods inside the container required to be static? For example, can I instead do $user = $reg->newUser(); ? Thanks.
      singulars
 

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