How to manage your and third-party dependencies using IoC container?

At the moment there is a library, where the bad instances are created from classes-dependencies directly in the designer.
Need:
1. put all third-party dependencies in the container
2. to put the library in container
3. to figure out how to implement the dependencies in the library and its classes
4. to use the class as before.

Don't understand how to use dependencies in their classes. While mind came one thought - to use a main App class that will be global to the NS library, and then using a singleton to a container object in the constructor of the dependent class.

$app = new Illuminate\Container\Container();
$app->bind('app', $app);
$app->bind('log', $log);


class A extends SplObjectStorage 
{

 protected $log = null;

 public function __construct()
{
 //how am I supposed to use a container that does not directly pass it to the constructor when you call?
 $this->log = new MongoLog(); 
}

}

class B extends A {}

class C extends A {}


$a = new A(); // ie, leave the call and using the library for what it is
September 19th 19 at 00:15
1 answer
September 19th 19 at 00:17
$app = new Illuminate\Container\Container();
$app->bind('app', $app);
$app->instance('log', $log);
$app->bind('MongoLog', 'log');

class A extends SplObjectStorage 
{
 protected $log;

 public function __construct(MongoLog $log)
{
 $this->log = $log; 
}
}

class B extends A {}

class C extends A {}

$a = $app->make('A');
$b = $app->make('B');
$c = $app->make('C');
ok. but if more "reality" example: classes and their use in different scopes... - Conner82 commented on September 19th 19 at 00:20
to apply each time to the container? strange ..... - Conner82 commented on September 19th 19 at 00:23
no matter what the scope is, it is important to register in the container the full FQN - buck_Bashirian commented on September 19th 19 at 00:26
: > to apply each time to the container? strange .....
Yes, but how were you planning on? To the container to intercept calls to `new {class}`? - buck_Bashirian commented on September 19th 19 at 00:29
: and what would be convenient :D - Conner82 commented on September 19th 19 at 00:32
call intercept classes from the specified NS) - Conner82 commented on September 19th 19 at 00:35
: I have not seen such magic in PHP - buck_Bashirian commented on September 19th 19 at 00:38
has a right to exist?
class IoC extends the Illuminate\Container\Container
{

/**
 * @var null
*/
 private static $singletonInstance = null;

/**
 * Returns an instance of the class (Singleton design pattern)
*/
 public static function getInstance()
{
 if (self::$singletonInstance === null) {
 self::$singletonInstance = new self();
}
 return self::$singletonInstance;
}

 private function __construct()
{
 $this->instance('app', $this);
 $this->instance('xml_writer', new XMLWriter());
}
}



class A extends SplObjectStorage
{

 protected $xml_writer = null;


 public function __construct()
{
 $IoC = IoC::getInstance();
 $this->xml_writer = $IoC->make('xml_writer');
}


 public function seeObject()
{
 echo '<pre>';
print_r($this->xml_writer);
 echo '</pre>';
}
}


$A = new A();
$A->seeObject(); //XMLWriter Object

if not, why not. - Conner82 commented on September 19th 19 at 00:41
there is no injection in the constructor, but it is not needed, because no environment (framework, controllers, etc.) no. - Conner82 commented on September 19th 19 at 00:44

Find more questions by tags LaravelPatterns of designingPHP