This is the use of the "Factory" pattern, I ask for help to understand if I use it correctly.

<?php Class HelloFactory { static $config = "My"; public static function createHello() { $fullNameClass = self::$config . "Hello"; return new $fullNameClass; } } Class CustomHello { public function view() { return "hello world!"; } } Class MyHello { public function view() { return "hello everybody"; } } $test = HelloFactory::createHello(); echo $test->view(); 

    2 answers 2

    Not quite right. The factory must create any object. For example extending one interface or abstract class.

     <?php class HelloFactory { static $parentClass = "Hello"; /** * @param string $className * @return Hello * @throws \Exception */ public static function create($className) { if(!class_exists($className)) { throw new \Exception("Класс $className Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½"); } if(!in_array(self::$parentClass, class_parents($className))) { throw new \Exception("Класс $className Π½Π΅ наслСдуСт абстрактный класс " . self::$parentClass); } $object = new $className(); // Π΄Π΅Π»Π΅Π³ΠΈΡ€ΡƒΠ΅ΠΌ ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΡŽ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° самому ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Ρƒ, ΠΈΠ½Π°Ρ‡Π΅ Π² Ρ„Π°Π±Ρ€ΠΈΠΊΠ΅ ΠΌΠ½ΠΎΠ³ΠΎ лишнСй Π»ΠΎΠ³ΠΈΠΊΠΈ ΠΈ ΠΊΠΎΠ΄Π° $object->init(); return $object; } } abstract class Hello { public $message; // инициализация ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° abstract function init(); public function view() { return $this->message; } } class HelloWorld extends Hello { public function init() { $this->message = 'hello world!'; } } class HelloEveryBody extends Hello { public function init() { $this->message = 'hello everybody!'; } } $test = HelloFactory::create('HelloWorld'); echo $test->view() . PHP_EOL; $test2 = HelloFactory::create('HelloEveryBody'); echo $test2->view() . PHP_EOL; // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ $test3 = HelloFactory::create('HelloAnyBody'); echo $test3->view() . PHP_EOL; 
    • Thanks for the detailed answer with the nested code. - user3771955
    • Static methods are not comme il faut =) - Dmitriy Simushev
    • @DmitriySimushev 1. Why? 2. This is a demo code, in a real project, of course, you can use DI and create a factory via services - korytoff
    • Problems start when you need to pass an object to the product designer. Then either static properties or bloated HelloFactory::create signature with DIP violation - Dmitriy Simushev

    In this implementation, your factory can only create objects of the MyHello class, and should create objects of different classes. Look at a small article , everything is clearly written there. Write the interface that the generated classes will implement and complete the description of the createHello() method using PHPDoc and everything will be fine.

    • Thanks for the answer. those. The object creation code will look something like this: public static function createHello($config) { $fullNameClass = $config . "Hello"; return new $fullNameClass; } public static function createHello($config) { $fullNameClass = $config . "Hello"; return new $fullNameClass; } public static function createHello($config) { $fullNameClass = $config . "Hello"; return new $fullNameClass; } Call accordingly: $test = HelloFactory::createHello('Custom'); echo $test->view(); $test = HelloFactory::createHello('Custom'); echo $test->view(); - user3771955
    • Yes everything is correct. - Zhukov Roman