Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Constructor Interfering With Another Class

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.

like image 999
SineCosine Avatar asked Apr 06 '12 16:04

SineCosine


2 Answers

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){
like image 69
kapa Avatar answered Oct 23 '22 10:10

kapa


Oy.

first, you are calling a non-static method as a static method.

B::B_runtime();

should only be used in this way if B_runtime were declared as a static method

static public function B_runtime(){

second, your class has an external dependency which is generally considered not good. a class should only rely on what is given to it, not on globals. this is called the dependency inversion principle. if a class is dependent on somthing, you should have to give this dependency to it via an argument, and even better, use type hinting to make sure that the dependency has the methods you expect it to.

public function A_runtime(B $object_b){

To take this a step further, you should also be using abstracts or interfaces for type hinting instead of concrete classes. this way you can switch B out with a different version of B if it ever becomes necessary.

interface BInterface {
   public function B_runtime();
}

then

public function A_runtime(BInterface $object_b){

read up on SOLID principles of OO design.

http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

like image 35
dqhendricks Avatar answered Oct 23 '22 09:10

dqhendricks