-1

Hi is it possible to call multiple $this in a single __construct()? I'm

relatively new to PHP OOP and MVC. Because I want to call a function in a

function so I thought of using the code below. Or is there any other way to

do better than this?

//maincontroller.php
class MainController{
   public $controller2;
   public $controller3;

   function __construct(){
      $this->controller2 = new Controller2();
      $this->controller3 = new Controller3();
   }
}

//controller2.php
class Controller2{

}

//controller3.php
class Controller3{

}
tereško
  • 56,151
  • 24
  • 92
  • 147
  • 2
    Constructors should not contain any computation and they definitely should not contain hardcoded classnames to some hidden and closely coupled dependencies. – tereško Jul 26 '14 at 17:17
  • Yes you can. Read this link: http://php.net/manual/en/language.oop5.php first. – Aziz Saleh Jul 26 '14 at 17:17
  • You might be confused by the OOP terminology but you're basically asking whether you can write more than one variable. You ran the code and worked as expected, didn't it? – Álvaro González Jul 26 '14 at 17:20
  • what do you mean by hardcoded classnames? what basically a constructor should contain @tereško ? – noobilityat19 Jul 26 '14 at 17:22
  • It should contain only the variable assignment for values that you have passed in. – tereško Jul 26 '14 at 17:30
  • with other words : __construct($controller2, $controller3) {} – DarkBee Jul 26 '14 at 17:31
  • @noobilityat19 - I believe that @tereško is suggesting that instead of using `new Controller2();` inside your MainController constructor, that you should either instantiate controller2 and controller3 instances outside your MainController and pass them as arguments to the constructor (Dependency Injection) or that you should use a controller factory to instantiate them – Mark Baker Jul 26 '14 at 17:31
  • Hi @MarkBaker can you show it to me how you do it? Sorry, i still don't understand it. – noobilityat19 Jul 27 '14 at 14:00
  • If you're just learning coding, then it is logical to instantiate your dependencies in your constructor. But doing so is an antipattern called "tight coupling" that makes your program more fragile and harder to extend later. When you're ready, read about [Dependency Injection](http://stackoverflow.com/a/140655/418413). – kojiro Jul 27 '14 at 14:40

1 Answers1

0

Using Dependency Injection:

//controller2.php
class Controller2{

}

//controller3.php
class Controller3{

}

//maincontroller.php
class MainController{
   public $controller2;
   public $controller3;

   function __construct(Controller2 $controller2, Controller3 $controller3){
      $this->controller2 = $controller2;
      $this->controller3 = $controller3;
   }
}

$controller2 = new Controller2();
$controller3 = new Controller3();
$controller = new Controller($controller2, $controller3);

Though the type-hinting is still to specific classes rather than to interfaces: it would be better if your controllers implemented an interface

Mark Baker
  • 199,760
  • 28
  • 325
  • 373
  • This answer could be improved by links to relevant articles and documentation about dependency injection. (While this answer makes a good example of the simplest form of DI, may future readers may benefit from understanding DI via proxy, configuration and object managers, inversion of control containers, etc.) – kojiro Jul 27 '14 at 14:44