Reputation: 29
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?
Upvotes: 0
Views: 72
Reputation: 8200
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;
}
}
Upvotes: 1
Reputation: 445
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.
Upvotes: 0