Not clear because no car class definition

Recommend this page to a friend!

      PHP Dependency Injection Container  >  PHP Dependency Injection Container package blog  >  Improving Your PHP Pr...  >  All threads  >  Not clear because no car class...  >  (Un) Subscribe thread alerts  
Subject:Not clear because no car class...
Summary:Not clear because no car class definition
Messages:4
Author:steven
Date:2015-11-03 11:01:18
 

  1. Not clear because no car class...   Reply   Report abuse  
Picture of steven steven - 2015-11-03 11:01:18
I think you forgot the car class definition in your article,
and thus I do not understand the advantage of the DI.
Can you be more precise ?

  2. Re: Not clear because no car class...   Reply   Report abuse  
Picture of Steve Steve - 2015-11-03 11:54:02 - In reply to message 1 from steven
Agreed!

Was interested in the article, though missing the explanation of the 'basics' - much like the start of the movie "The Martian" - several holes in the story, like the explanation of why the crew would go 'exploring' during a storm so intense they had to leave Mars......

  3. Re: Not clear because no car class...   Reply   Report abuse  
Picture of Kayla Anderson Kayla Anderson - 2015-11-03 12:43:50 - In reply to message 1 from steven
I'm glad it wasn't just me. I read it and it sounded like "Dependency injection is about injecting dependencies and it's useful because it injects dependencies. Here's some code that shows you how to set a property on a class."

  4. Re: Not clear because no car class...   Reply   Report abuse  
Picture of Samuel Adeshina Samuel Adeshina - 2015-11-03 19:32:04 - In reply to message 3 from Kayla Anderson
Think of an instance where you want to create a database query class (or service)
You need to provide a connection to your database, this is what the query class uses to "make the necessary connections to the DB and executes queries successfully". Usually, a separate class handles the connection.

You can pass your connection class into your query class as a dependency, in other words, you inject the connection class (the dependency, a service or factory needed by other objects or services) into your query class which is the object that's dependent on another object.

<?php
class connection
{
public function conn()
{
//perform connections to the database
}
}

class query
{
public function __construct(connection $connObj)
{
$connObj->conn()
//we've just established a connection to the DB
}
}

?>

The above code snippet is a better illustration of the DI concept. Think of the connection class as the "automobile object" and the query class as the "car object" for a "car class" analogy.

Watch out for the next part of the article that talks about how to properly inject objects, more on the single responsibility principle and how to build a DI container like Pimple.

 
For more information send a message to info at phpclasses dot org.