I am teaching myself PHP and am playing around with different projects to learn as much as possible. Right now I am working on a login script and everything works as expected. I originally wrote the script with just functions and no classes but then decided to incorporate classes to help myself learn them. I need some input on the following class. I don't feel switching to a class has accomplished anything because I simply inserted the function into the class. I know there is a better way to do this but am having trouble figuring it out.
Also, in each function I call the database connection class to establish a connection, is there a way to extend the database connection and use a variable for the connection so I don't have to call the class for each and every function?
I tried
<?php
include('dbClass.php');
class login_user extends Database {
public $db = Database::getInstance();
public $conn = $db->getConnection();
and I get the following error
Fatal error: Constant expression contains invalid operations in /Applications/MAMP/htdocs/login_project/includes/classes/login_userClass.php on line 5
dbClass.php
<?php
class Database {
private $_connection;
private static $_instance; //The single instance
private $_host = SERVER;
private $_username = USER;
private $_password = PASS;
private $_database = DB;
/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}
// Constructor
private function __construct() {
$this->_connection = new mysqli($this->_host, $this->_username,
$this->_password, $this->_database);
// Error handling
if(mysqli_connect_error()) {
trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
E_USER_ERROR);
}
}
// Magic method clone is empty to prevent duplication of connection
private function __clone() { }
// Get mysqli connection
public function getConnection() {
return $this->_connection;
}
}
login_userClass.php
<?php
class login_user {
//see if the user exists
function does_user_exist($username){
$db = Database::getInstance();
$conn = $db->getConnection();
$stmt = $conn->prepare("SELECT user_name FROM users WHERE user_name = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows;
}
//get the user hash and if create session variables and log users in.
function get_user_hash($username, $password){
$db = Database::getInstance();
$conn = $db->getConnection();
$stmt = $conn->prepare("SELECT id, user_name, user_password, user_email, user_registered FROM users WHERE user_name = ? LIMIT 1");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($id, $user, $pass, $email, $user_registered);
while($stmt->fetch()){
if(password_verify($password, $pass)){
$_SESSION['user_id'] = $id;
$_SESSION['user'] = $user;
$_SESSION['email'] = $email;
$_SESSION['registered_on'] = $user_registered;
$_SESSION['loggedin'] = true;
header("Location: http://".$_SERVER['HTTP_HOST']."/user.php");
exit;
} else {
return false;
}
}
}
}