There is an interface that will be common to all objects:

interface Config_Interfacefunction { public function read($file){ echo 'read'.$file; } } 

It is necessary to create several types of objects, each type of behavior which we describe in classes:

  class Config_Config { public function Con(){ echo 'conf'; } } class Config_Config2 { public function Con(){ echo 'conf2'; } public function Joi(){ echo 'Joi'; } } 

Now I want a general class that will create objects of different types, depending on the passed argument.

 class Con { public $root=19; public function __construct($instance) { ..... } } $obj=new Con(new ......);//что здесь писать??? 

    2 answers 2

    Wikipedia has a great article with examples, including for PHP.

      Template "Strategy" is not entirely appropriate. A strategy is well suited in cases where it is the choice of a particular strategy (cap) that is required. For example, you have a base abstract class with the Say() method, three classes are inherited from it with different implementations of the Say() method, and finally, you have a strategy sampling class, to the constructor of which the object of our base abstract class is passed, in this The class also has a delegating Say () method. This is a typical strategy.


      In your case, the Factory Method pattern would be most appropriate. It is intended to create class objects, depending on the situation.