So in php im trying to get a public variable in the database class that connects to a database when the class is created. Like so -
<?php
class database {
public $_link;
public function __construct (){
$this->_link = new PDO("mysql:host=localhost; dbname=swinkidc_student", "swinkidc_student", "");
}
}
and...
<?php
class user{
private $db;
public function __construct() {
$this->db = new database;
}
/**
* Returns the ID of a user.
* @param string $user
* @return mixed
*/
public function getUserID($user){
$query = $_link->prepare("SELECT `user_id` FROM `users` WHERE `username` = :user");
$query->bindParam(":user", $user);
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
return $result['user_id'];
}
/**
* Checks if given user is active.
* @param string $user
* @return bool Returns true/false if user is active.
*/
public function isUserActive($user){
}
}
If I extened database in user I can obviously refer to _link if i make it private and it will work, however, I don't think that i should inherit just for getting something like that..
Whenever i try the about i get : Fatal error: Call to a member function prepare() on a non-object in /home/swinkidc/public_html/studentreach/core/authentication/user.php on line 18
As said, if i try the inheritance way it will work, I just don't feel like thats a great way of doing something like this.
Any suggestions please?
Thanks!