M
M
movetz2015-06-08 13:23:24
PHP
movetz, 2015-06-08 13:23:24

How to “beautifully” initialize an object property?

I apologize in advance for the stupid question, but I am tormented by vague doubts that I am doing something wrong, there is such a case:

class Product {
         //Some properties
        //....

         //  \Models\OptionsInterface
         protected $options;

          //  \Models\PriceInterface
         protected $price;

         protected $type;         

         public function setOptions($options) 
         {
                if($this->type == ...some_condition_a && ...next_condition_a...)
                {
                     $this->options = $this->factory->make('OptionsA', $options);
                }
                elseif($this->type == ...some_condition_b &&  ...next_condition_b ..)
                {
                     //Some options logic
                     $options = ....
                     $this->options =  $this->factory->make('OptionsB', $options);
                }
                //Также объект может и не иметь опций, в этом случае
               //$this->options = null по-дефолту
         }

         public function setPrice($price)
         {
                $this->price = $this->factory->make('Price', $price);
         }
}


class ConcreteProduct extends Product {
    
         public function setOptions($options) 
         {
                if($this->type == ...some_condition_a && ...next_condition_a...)
                {
                     //Some options logic
                     $options = ....
                     $this->options = $this->factory->make('ConcreteOptionsA', $options);
                }
                elseif($this->type == ...some_condition_b && ...next_condition_b...)
                {
                     //Some options logic
                     $options = ....
                     $this->options =  $this->factory->make('ConcreteOptionsB', $options);
                }
         }

          public function setPrice($price) 
          {
                if($this->type == ...some_condition_a)
                {
                      //Some price logic
                     $price = ... 
                     $this->price = $this->factory->make('ConcretePriceA', $price);
                }
                elseif($this->type == ...some_condition_b)
                {
                     $this->price = $this->factory->make('ConcretePriceB', $price);
                }
                else
                {
                      $this->price = $this->factory->make('Price', $price);
                }
         }
}

//Работа с объектом осуществляется в таком роде
$product = new ConcreteProduct();
$product->setOptions($data['options']);
$product->setPrice($data['price']);

The problem lies in the initialization of the $options and $price properties, the number of Product descendants and $types may increase in the future, as a result of which the $options property will have many implementations. I also don’t really want to multiply the Product’s heirs by type, as well as fence a bunch of branches. There is an idea to create a factory for options, but here's how to describe the logic for choosing the right class ... there is also an idea to create a builder for products depending on the type, but there are also doubts here ... I'm already completely confused ((
Many thanks in advance))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2015-06-08
@Adamos

Just implement $options and $price as separate classes.
In Product, add the members of the appropriate classes, and in Product, use only their generic logic. Everything that can differ is inside the classes themselves.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question