0

I currently have a class called Connect with the following code:

class Connect
{
public $sqlHost='host';
public $sqlUser='user';
public $sqlPass='pass';
public $sqlDB='db';

public $db;
    public function __construct() {
        $this->db = new mysqli($this->sqlHost, $this->sqlUser, $this->sqlPass, $this>sqlDB);
    }
}
?>

I also have a class called TODO and I was wondering, how could I go about calling $db located in the Connect class from the TODO class?

2 Answers 2

1

imagine you have have two objects called

$connect = new Connect();
$todo = new TODO();

now 1 of 3 things can happen, You can pass the $connect object into a method of $todo or if a Connect object is a member of a TODO object, or create a new connect object.

scenario 1:

class TODO {
    public function foo($connect){
        // You can get the db object here:
        $connect->db
    }
}

$todo->foo($connect)

scenario 2:

class TODO {
    public $connect;
    public function __construct(){
        $this->connect=new Connect(); 
    }
    public function foo(){
        //get db:
        $this->connect->db;
    }
}
$todo->foo();

scenario 3:

class TODO {
    public function foo(){
        $connect = new Connect();
        $connect->db;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure what you want, but I think its this

$connectInstance = new Connect();
$connectInstance->db...

This way you can access the db variable in a connect object. But first, you have to instanciate the object.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.