I apologize. Who dealt with this pattern, I ask for help. What is its purpose and where can it be applied in PHP + mysql?
- In short: the adapter allows you to bring one interface to another for future use. - AseN
- Council number 0. If the pattern is well needed - do not apply. - istem
- Wikipedia example is gorgeous: the link to the object is transmitted and its properties are used ... I wonder what relation it had to what is written there .. A brilliant example. - zloctb
2 answers
As mentioned above, the adapter allows you to solve the problem of incompatibility of interfaces. Suppose you have two classes that are not connected by a common inheritance hierarchy and do not implement the same interfaces, which makes their interaction more difficult. In this case, some kind of wrapper class that implements the interface of the first class will be useful and by composition includes the second one whose methods can be invoked in the methods of the wrapper class. You can read more about it here and here.
Yes, and more:
What is its purpose and where can it be applied in PHP + mysql
If you are going to apply a pattern just for the sake of applying a pattern, then this is a very stupid idea.
class Product{ private $prix; function __construct($prix){ $this->prix = $prix; } function getPrix(){ return $this->prix; } } class ProductAdapter(Product $product, $discount){ function getPrix(){ return $this->product -> getPrix() - $discount; } } $product1 = new Product(1000); $product1-> getPrix (); $productDiscount = ProductAdapter($product1, 300); // цена со скидкой $productDiscount -> getPrix ();