cleanup constructors method

42 views Asked by At

I have a class with a large number of parameters in the construct method

class Foo {
  public $d1;
  public $d2;
  public $d3;
  ...
  public function __construct($d1,$d2,$d3,$d4,$d5,$d6)
  {
   $this->d1 = $bar->d1;
   $this->d2 = $bar->d2;
   ...  
  }
}

I want to clean the code in method __construct

my try :

class Bar {
  public $d1;
  public $d2;
  public $d3;
  ...
}

class Foo {
  public $d1;
  public $d2;
  public $d3;
  ...
  public function __construct(Bar $bar)
  {
   $this->d1 = $bar->d1;
   $this->d2 = $bar->d2;
   ...
  }
}

In this structure, I want to observe two important points :

1 - clean code
2 - Whenever someone wants to make an object of this class, he must make it according to the structure of the Bar class

Is my structure correct? Do you have a better offer?

0

There are 0 answers