Dependency Injection - Passing Objects Around - php

I'm currently creating something based on OOP principles and I'm trying to apply dependency injection. I'm aware that I'm possibly doing something wrong here, the whole DI thing seems to be more confusing that it probably is but I'm still struggling to quite get my head around it.
I've created a Form class which will output various form elements but the form class will need at least two other classes (created as objects) to function properly.
Here's how I've got it:
$config = new Config();
$db = new Database();
$html = new HTML();
$form = new Form($config, $db, $html);
This is just me playing around so Form may not need all of those things but I'm using them to illustrate the point.
Now, when I'm creating a form, I don't want to have to pass the three objects into it to use it. I could solve this with static methods etc but it's really not a path I'm wanting to go down.
So what's the best way to be able to use the three objects created earlier in the Form class?
I guess I'm wanting an almost global type of behavior to a certain extent.
I've found a couple of things talking about this and factories, IoC containers etc but nothing that explained it clearly and easily so any help with this would be great. Or even a link to an easy to understand tutorial or something as I've failed to find one myself.
I also found Pimple - could something like that be along the right lines?

Well, if you don't pass the three objects in, you are not doing dependency injection.
To avoid the parameters again, you can write either a Wrapper class, or a factory class which has methods to create and return a form object. You could initialize rhe factory class with $config, $db, $html once and then use that for every form created.
So the process would look like this:
$factory= new Factory($config, $db, $html);
$form= $factory->createForm();

Related

How do you Inject Valitron with Pimple and reuse

So I've been using the Valitron Library to validate posted forms mostly and have run into some issues.
The constructor accepts the data to be validated and this causes problems when you inject the library as a dependency with Pimple or some other container.
It also causes issues if you want to validate multiple things, as you basically have to instantiate the library every time you want to use it.
Is there some way around this?
Ultimately I would like to be able to define the library as a service and inject it with Pimple like this:
$container['Valitron'] = function(){
return new \Valitron\Validator();
};
Any controller/class that needs to validate something would initialise it in their constructor like this:
public function __construct($valitron)
{
$this->valitron = $valitron;
}
Any time I need to validate something I could say something like:
// First use
$this->valitron->setData($_POST);
$this->valitron->rule('required', 'name')->message('Im required')->label('Name');
$this->valitron->validate();
// Second use
$this->valitron->setData($_GET);
$this->valitron->rule('required', 'test')->message('Im also required')->label('Test');
$this->valitron->validate();
But there doesn't seem to be a setData function, or any way to reset the library between usages.
Question:
How do I use Valitron with Pimple and reuse it for validating multiple things at a time?
Please Note: It must be injected. It also should not need to be initialised before each usage. Please don't tell me I have to extend the library or hack it to make it work properly!
Came across your question when I was searching for the same as you did bit I came also across the following Github issue in Valitron's repo, see https://github.com/vlucas/valitron/issues/108
vlucas wrote: Valitron is currently designed to be a one-use instance, so it could lead to weird things like custom labels and error messages not getting reset between validations (because it was never meant to be used that way).

How to build a PHP Dependency Injection Container

I've recently learned about the advantages of using Dependency Injection (DI) in my PHP application. However, I'm still unsure how to create my container for the dependencies, or whether I should be using DI at all for the online forum that I'm building.
The following code is my version of the DI container I have made based on the example I learned from here .
class ioc {
var $db;
var $session;
var $user_id;
static function newUser(static::$db, static::$user_id) {
$user = new User($db, $user_id);
return $user;
}
static function newLogin(static::$db, static::$session) {
$login = new Login($db, $session);
return $login;
}
}
$user = ioc::newUser();
$login = ioc::newLogin();
I have a few questions:
1) 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.
2) 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. For example:
class Users {
function __construct($db, $user_id) {
$this->db = $db;
$this->user_id = $user_id;
}
function create_friends_list() {
$st = $this->$db->prepare("SELECT user_id FROM friends WHERE user_id = $this->user_id");
$st->execute();
while($row = $st->fetch()) {
$friend = ioc::newUser($row['user_id']);
$friend->get_user_name();
$friend->get_profile_picture();
}
}
}
3) 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.
It seems to me that DI creates a lot of overhead and there are situations where it is unusable (as in my #2 example). The following site is from a developer who cites many good reasons not to use DI. Does his arguments have any merit? Or am I just using DI wrong?
check this link.
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.
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 ($ioc = new IOC();) then you need some kind of service provider class say
$ioc->register('database', new DatabaseServiceProvider($host, $user, $pass))
Now every time you want a connection to the database you just need to pass in $ioc->get('database'); 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 $ioc with totally different settings making it easy to create connections to say a different database for testing purposes.
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.
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
$ioc->register('login-user', User::fetch($ioc->get('database'), $user_id));
so now $ioc->get('login-user') returns your logged in user. You can then use User->fetchAll($ioc->get('database')); to get all your users.
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.
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
I hope you understand my answers but if you need any more info let me know.
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).
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.
class ioc
{
public $db;
// Only pass here the things that the class REALLY needs
static public function set($var, $val)
{
return $this->$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->db, $session);
}
}
if (ioc::set('db',ioc::newDB($user, $pass)))
{
$user = ioc::newUser($user_id);
$login = ioc::newLogin($session);
}
2.- You shouldn't do $friend = ioc::newUser($row['user_id']); inside your class. There you are assuming that there's a class called ioc with a method called newUser(), while each class should be able to act on it's own, not based on [possibly] other existing classes. This is called tight coupling. 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: The Clean Code Talks - Don't Look For Things.
I'm not sure about how the class User behaves, but this is how I'd do it (not necessary right):
// Allow me to change the name to Friends to avoid confusions
class Friends
{
function __construct($db)
{
$this->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->$db->prepare("SELECT user_id FROM friends WHERE user_id = ?");
$st->execute(array($user_id));
$AllData = $st->fetchAll()
return $AllData;
}
else return null;
}
// Pass the $friend object
function get_friend_data($friend)
{
$FriendData = array ('Name' => $friend->get_user_name(), 'Picture' => $friend->get_profile_picture());
return $FriendData;
}
}
$User = ioc::newUser($user_id);
$Friends = new Friends($db);
$AllFriendsIDs = array();
if ($AllFriendsIDs = $Friends->create_friends_list($User->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->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->get_user_name();
$Friend->get_profile_picture();
}
I didn't test it, so it has probably some small bugs.
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.
PS, sorry, I changed your bracket style everywhere.
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.
Instead of globals in your init.php define your objects like:
ioc::register('user', function() {
return new User();
});
And inisde your create_friends_list method use:
ioc::get('user')->newUser($user_id);
This is a really plain implementation. Check out:
http://net.tutsplus.com/tutorials/php/dependency-injection-huh/?search_index=2
or
http://net.tutsplus.com/tutorials/php/dependency-injection-in-php/?search_index=1
for more information.
The question you asked has one very important catch, which I asked about in a comment. Any time you examine the possibility of pausing forward progress in order to go back and accomplish a less than trivial re-factoring of existing classes, you have to really think about the payoff.
You replied:
It seems a bit complex (and very different) and more work than it's worth to implement at this point. I'm just trying to hack out something real quick and see if it gains traction.
Remember that you have the following things to do if you want to implement an DI with an IoC container properly:
Implement the IoC container and registry at bootstrap
Modify all existing classes that have dependencies to allow setter injection, while not depending on the IoC container itself
Re-writing / Re-structuring of your tests as needed
A note on the second bullet which reflects my own personal view and preference, developing with DI in mind can be hard and you want the full reward it can give. One of the biggest rewards is completely decoupled objects, you don't get the complete part of that if everything still depends on an IoC container.
That's a lot of work, especially if you haven't been considering the pattern until now. Sure, you'll have clear benefits, such as lots of re-usable and easily testable objects when you're done. But, as with all re-factoring, nothing new would be accomplished in the context of adding or finishing new functionality and features. Is difficulty testing or tight coupling getting in the way of that? That's something you'd have to weigh.
What I suggest you do instead is keep the pattern in mind as you write new code. Provide setter methods to inject dependencies, which can be utilized manually or through an IoC container. However, have your classes continue to create them just in time if none have been injected and avoid composition in a constructor.
At that point you'll have a collection of classes that lend much better to inversion of control, if that's a pattern you want to pursue in the future. If it works well for you and it's something you really want to incorporate into your design decisions, you can easily re-factor to remove the JIT logic later.
In conclusion, I'm afraid that the only thing you'll end up with if you try implementing it completely, and properly right now is a mess. You can change how you write classes now going forward, but I wouldn't go back and try to implement it across the board.

Puzzled with Dependency Injection in PHP (chains of Classes)

I'm moving from Globals and Singletons (=bad?) to Dependency Injections (=good?) in PHP and I'm very new to it.
I have read many related topics on Stack Overflow already but I still can't understand the main principles of DI.
Please tell me if I'm doing it right (it's just shortened pseudo-code):
// first connect to DB
$sql = new Sql();
// create logger (I'm writing logs to Database so I need to pass $sql to it)
$log = new Log($sql);
// restore logged in user, get info about user, unread messages...
// class "User" needs access to Database and Logs:
$user = new User($sql, $log);
// now we need to collect all the data of current section of my Website
// I'm using Model and Controller:
$model = new FrontPageModel($sql, $user, $log);
$pageController = new FrontPageController($model);
Though it may look OK at this step but what if I need to get access to more classes like Config, Session, etc.?
Should my code transform to this?
$model = new FrontPageModel($sql, $user, $log, $config, $session);
Isn't it too much already?
I know someone could advice to use a kind of big "Application" class and put Config, Session, Log, Db objects inside of this class but I feel that it isn't very good idea.
Next question - what if I need to get logged User's ID inside of my FrontPageController? I did not pass an instance of "User" to FrontPageController, but it was passed before (in chain) to FronPageModel.
class FrontPageController{
private $model;
function __construct($model){
$this->model = $model;
}
function getData(){
echo $this->model->user->id; // is it right way?
}
}
That "$this->model->user->id" seems like overkill to me.
It might not be the prettiest, but you're certainly not doing it "wrong". You're demonstrating classing constructor injection, but you could perhaps refactor to keep some of the disparate objects separated a bit more.
My suggestion would be to look at established PHP DI containers. See how they work, use them in a few apps, and (1) you'll have much more testable apps and (2) you'll be much more comfortable with DI in general.
Take a look at one or more of the following:
Pimple
Aura DI
Symfony DI
Like all the joyful stuff, it' depends.
I would put everything inside a factory class, or several factory classes for different sub systems.
class AppFactory {
protected $_sql;
public function getSql()
{
if (!empty($this->_sql))
return $this->_sql;
$this->_sql = new Sql();
}
}
Then initial it on application run. Configure if needed.
$factory = new AppFactory();
// set config
// $factory->internallyGenerateSingletons = true;
And now every other class can use it. And of course you have injection to put factory inside.
$model = new FrontPageModel($factory);
// or
// $model->useFactory($factory);
Regarding other question. Is this the right way.
...
$this->model->user->id
...
I would say it's ok but batter is to use methods like this:
...
$this->model->getUser()->id
..
So you can mock user class when doing unit testing. And it makes application more flexible.
It seems like you have got the idea of dependency injection right. You even question Symfonies idea of DI (in a comment), which shows more mature understanding.
A next step to solving the questions you have is to take a step away from leaking technical information from one layer into another. Eg your controllers don't care that they talk to a model. Your model doesn't care it talks to an sql or nosql database.
Another is about responsabilities. Does the user really need to do all of that? Is the user responsible for logging? It may be a correct choice, but it also seems that you ask it to do a lot of things. Does the user construct itself, update itself? etc
$this->model->user->id; // is it right way? Is not the right way. 1) This is very strong coupling. 2) do you need the id of the user? is the user only put in as dependency to get something from it? perhaps something like $this->user->showUserInfo($userInfoDisplay) can be used?

Dependency-injection in real life

I am building a really minimal MVC framework to increase my PHP knowledge and challenge myself. I've come to the point where Classes begin to be dependent on each other to work.
Dependency injection seems to be the solution to this and are used by some of the big frameworks around.
I've found Bucket on Github and have messed around it for a while to understand the basics. What I can't get my head around however is when it's appropriate to create a Container?
Making one big container including every possible class that may be needed seems nothing but counter-productive to me and I can't imagine that it is good practice. It seems like the recipe for bad performance at least.
In the alternative, which is to make multiple containers, I still don't get how the ever-stinking Singletons are not needed anymore.
Let's say that I would have the following code:
$session_container = new bucket_Container();
$session_container->create('Database');
$session_container->create('Database_Sessions');
$log_container = new bucket_Container();
$log_container->create('Database');
$log_container->create('Database_Log');
So here we have two containers, or in this case buckets for two totally different usages which are mutual by their dependency on the Database class.
My logic tells me that the above code will create two independent instances of the Database-class, meaning that I still would have to make the Database-class a singleton to ensure that concurrent instances of my database connection isn't occurring?
Is this correct?
I don't know much about the specific lib, but assuming it lets you use a factory, let the factory return the same instance.
Edit: Ok, this is simply on the Bucket GitHub index page.
class MyFactory {
function new_PDO($container) {
return new PDO("mysql:host=localhost;dbname=addressbook", "root", "secret");
}
}
$bucket = new bucket_Container(new MyFactory());
$db = $bucket->get('pdo');
So in your case you could simply do:
class MyFactory {
private $pdo;
function new_Database($container) {
if($this->pdo){
return $this->pdo;
}
return $this->pdo = new PDO("mysql:host=localhost;dbname=addressbook", "root", "secret");
}
}
$factory = new MyFactory();
$session_container = new bucket_Container($factory);
$session_container->create('Database_Sessions');
$log_container = new bucket_Container($factory);
$log_container->create('Database_Log');
Something like that. Doesn't seem like rocket science.
Edit2: I don't have enough rep points to comment on the question (bit silly), but in response to your "modularity" concern: think of the container as the "glue" of your application. Indeed, if you have a large application, you may want to "glue" inside an isolated part of your application only. That is a valid encapsulation concern. But even then you still need a container that handles injection at the highest level of abstraction. If you just create a separate container for every part of your application, you either end up with unneeded duplication of instances, or you have to apply another level of instance management, which doesn't improve the encapsulation in any way: you're still sharing instances between different parts of your application.
My advice would be to use a single container at the bootstrap level. If you want added encapsulation for specific parts of your application (modules, plugins, whatever), use "child containers". A child container inherits the instances from the parent container, but the parent container knows nothing of the child (as far as he's concerned, he's still a bachelor ;)). Could be that Bucket supports this by default, I know other DI containers do. If not, it's really easy to implement using a Decorator. Imagine something like this:
class MyContainerType extends bucket_Container {
private $_parent;
private $_subject;
public function __construct($factory = null, bucket_Container $parent = null){
$this->_parent = $parent;
$this->_subject = new bucket_Container($factory);
}
public function get($key){
$value = $this->_subject->get($key);
if($value){
return $value;
}
return $this->_parent->get($key);
}
/**
* Override and delegation of all other methods
*/
}
Making one big container including every possible class that may be needed seems nothing but counter-productive to me and I can't imagine that it is good practice. It seems like the recipe for bad performance at least.
On the contrary. This is exactly what you would do with a di container. The container will only instantiate objects on demand, so there is virtually no overhead to managing all you singleton-ish classes through it.
The biggest problem with di is to distinguish between shared objects (Things you would usually think of as singletons) and transient objects (Objects that have plenty of instances through a normal application flow.). The former are easily managed through di. The latter don't really fit. Having those two "kinds" of objects clearly distinguished may seem like a bit of a hassle, but is really a highly beneficial side effect of using a di container.
If you are worried about multiple simultaneous connections you can just use mysql_pconnect() or the equivelant for the database you are using. It will check if a connection is already open and use the existing connection if it is.
As far as the container issue, I've seen it done in two ways, which you seem to be aware of both. The first method is to have the framework read your database schema and create classes fore each table. I personally don't like this approach. Symfony is one framework that does this (by using the doctrine ORM).
The more preferred method I've seen is to have a generic container, which basically builds the sql for you given a table, columns and an action. This is the approach taken by codeIgniter:
$query = $this->db->get('mytable');
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

General Programming: When a set of objects are managed by another object, what do you call it?

I'm looking for help identifying this design pattern and learning the "typical" vocabulary it uses:
I'm working on a project in PHP, and I've created a thin ORM layer that saves generic objects to and from the database. There are two classes that do the work:
"my_object" is basically a container for various kinds of data. After being created this object can save itself to the db.
"my_object_manager" is used to manage a set of objects, for instance if you wanted to retrieve many of them and iterate through them.
As a simplified example, you could do something like:
$post = new my_object('post');
$post->title = 'foo';
$post->body = 'bar';
$post->author = 'baz';
...and you if you wanted to load a bunch of posts you could do something like:
$posts = new my_object_manager('post');
$somePosts = $posts->getBy('author','baz');
foreach( $somePosts as $aPost ) {
...loop stuff here...
}
So, my question is this: In the class definition for "my_object_manager" I need to store a property that identifies what kind of object is being managed. It looks something like this:
class my_object_manager {
protected $theKindOfObjectThatThisManages;
function __construct($whatToManage) {
$this->theKindOfObjectThisManages = $whatToManage;
}
}
Now, forgive me for not knowing this kind of basic stuff, but I'm self-taught and have a pretty limited programming vocabulary. I'm sure this kind of design pattern is common, but for the life of me I haven't been able to figure out what it's called.
I'm trying to write code that other programmers can read and understand, SO, my real question is if you were reading this code what would you expect "$theKindOfObjectThatThisManages" to be called? What is this program design pattern called, and what do you call this kind of an object if you want other programmers to know what it's doing?
Lastly, the question editor popped up and told me that this question looks subjective and is likely to be closed. I hope that this question is, in fact, ok for Stack Overflow - but if not, where could I ask this question and get an answer?
Thank you!
For your code samples, I would use
class my_object_manager {
protected $my_object_type;
function __construct($whatToManage) {
$this->my_object_type = $whatToManage;
}
}
Now, you seem to be following closely the Active Record pattern, of which many implementations exist, you can go see how they do it in practice :)
Usually, you don't provide access to _manager() objects, but make my_object()s inherit from it. So, you'd have something like
$posts = new Posts(); //Where Posts() extends my_object_manager
$somePosts = $posts->getBy('author','baz');
foreach( $somePosts as $aPost ) {
...loop stuff here...
}
Check out:
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)
Your object manager's design, depending on how complex it gets, may fall under several patterns. For example:
Composite: Maybe your object manager allows you to run updates on multiple objects at once (typical database scenario) using the same interface. You can alter individual objects in the same way you'd alter collections.
Facade: If your object manager provides methods to combine various parts of your system into a single, easier to use interface then it's using the Facade pattern. For example, maybe your object manager creates a 'user' object and automatically generates their first 'post' via a single API call. Perhaps you could have used create() functions on the post and user objects to do this yourself, but the Facade pattern combines them (and perhaps helps to deal with concurrency issues as well) into an easier API since these operations are commonly called together.
Mediator/Observer: Your object manager might be responsible for observing changes to objects and handling the "mediation" between them. Your object manager might provide a method for commenting on a post. But when this method is called, the author of the post might need to be notified via email. So your object manager could be responsible for communicating between the relevant objects and notifying a listening email service by sending an event or similar message.

Categories