I'm still kind of new to PHP OO programming techniques and I have a very simple broad question, is it generally bad practice to instantiate a class in a class then pass that instance to another class?
What I want is to be able to create an instance of a specific class I know I will always need through each user request. Class two is more than anything just a helper class, ideally in my application it would be a "loader" that loads the views.
First class which calls the other two classes:
require 'classTwo.php';
require 'classThree.php';
class first {
public $classTwo, $classThree;
public function __construct() {
$this -> classTwo = new classTwo;
$this -> classThree = new classThree;
$this -> classThree -> displayNum( $this -> classTwo );
}
}
Second class which is the helper class:
class classTwo {
public function returnVal() {
return 123;
}
}
Third class is the action class where actual work and final display would happen:
class classThree {
public function displayNum( $instance ) {
echo $instance -> returnVal();
}
}
Overall I just want to know if this is bad practice because I never seen it done before.
Thanks in advance!