E
E
Eugene Pukha2015-10-25 15:47:45
PHP
Eugene Pukha, 2015-10-25 15:47:45

How to implement such a mechanism for the service container?

Good day everyone.
The situation is this:
1. There is a certain service container.
$conatiner;
2. We put some class in it, for example phpMailer

$conatiner->set('mailer',function(){
  return new phpMailer();
})

3. The logic of the container is simple, it helps to postpone the creation of an object until its first call. Those. when we need to send an email, we do something like this:
$mailer = $conatiner->get('mailer');
// тут задаём необходимые параметры класса phpMailer
$mailer->send();

4. When resubmitted, the code is the same, but the container does not create a new instance of the class, but returns the one that was created during the very first access to this element of the container.
There is also a class that also works with sending mail. An instance of the phpMailer class must be passed to its constructor, because he uses it to send letters. Now here's the question. How can you implement the work of the container in such a way that instead of an instance of phpMailer you can pass a link, or any pointer to the container element, but do not call the constructor of this class. Those. to postpone the creation of an instance of the phpMailer class in the container until the first call to it from the MyMailerClass () class??
$someMailer = new MyMailerClass(new phpMailer());
It is highly desirable that the MyMailerClass class does not differ by passing it an instance, or a reference to the container element.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Melekhovets, 2015-10-25
@summoner2015

symfony.com/doc/current/components/dependency_inje...

A
Alexander Taratin, 2015-10-25
@Taraflex

Pass the class name as a string.
https://stackoverflow.com/questions/4578335/creati...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question