Class Constructor Interfering With Another Class
class, constructor, function, php, variables
Solution
You could pass the instance of B to the method. This way you also define the dependancy of your method on class B.
$A = new A($db);
$B = new B($db);
echo $A->A_runtime($B);
class A{
//...
public function A_runtime($instance){
return $instance -> B_runtime();
}
}
You can even use type hinting in PHP 5 to signal that you are expecting an instance of class B there:
public function A_runtime(B $instance){
Problem
Essentially, I am just creating two classes where one class, in this case class A, runs a function in another class, in this case class B, to grab some information from the database. However, when the `B_runtime()` actually calls upon the database I get the error `Cannot access protected property A::$db`. What I don't understand is that even though I have two `__construct`'s in both classes the PDO statement is being very persistent on using the database connection from class A. I am sure this has something to do with the fact that I am running the `B_runtime()` from within class A because this doesn't happen if I call it from outside of class A. I know that I can simply change the `protected $db;` in class A to a public variable, however, I am really curious as to how I can fix this. ``` ob_start(); include('/config.php'); ob_end_clean(); $A = new A($db); $B = new B($db); echo $A->A_runtime(); class A{ protected $db; public function __construct($db){ $this->db = $db; } public function A_runtime(){ return B::B_runtime(); } } class B{ protected $db; public function __construct($db){ $this->db = $db; } public function B_runtime(){ $preparedStatement = $this->db->prepare('SELECT * FROM z_mod_html WHERE ModuleLink = :moduleid LIMIT 1'); $preparedStatement->execute(array(':moduleid' => '1')); $rows = $preparedStatement->fetchAll(); return $rows[0]['HTML']; } } ``` Sorry for the long amount of code - if anyone has any ideas or suggestions it would be greatly appreciated. Thanks.