T
T
topuserman2021-11-22 13:21:37
PHP
topuserman, 2021-11-22 13:21:37

Builder: do you need a factory, or can you leave a method in the builder?

I have some Logger class that is quite difficult to instantiate to make things easier,
I created a simple builder that helps to quickly configure and create a logger object.

But in an application quite often where the same builder code can occur.

For example:

$logger_builder = new LoggerBuilder('logger_name');
$logger = $logger_builder->setLogLevel(Logger::ERROR)->setChannel(Logger::DEFAUL_CHANNEL)->setFormat('...');
In order not to write this paw every time, I want to wrap this code in a method and use it already.
Where it is better from the point of view of structure to put this method?
Do I need to create a separate factory (I think this is superfluous ..?)?
Or you can put it in the builder itself (I think this is not his area of ​​responsibility)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2021-11-22
@glaphire

1) create a wrapper class for this logger, configure it inside (in the init method, put the init method into the constructor)
2) study the implementation of the service container in the current framework / application and pass the already configured logger through dependency injection to the necessary classes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question