Reputation: 1604
I'm trying to make the transition to OOP PHP to help clean up the cluster of code I'm currently working with.
I'm using PHPass to hash passwords in my database but with this OOP approach, I can't get my head around how to call it in my class' login function.
As far as I can see, in all the places I've tried calling it, it's always declared before my class is initialised but it's still telling my it's undefined or a non-object.
db_config.php
...
require_once("PasswordHash.php"); // Location no.1 to try it
$password_hash = new PasswordHash(8, FALSE);
include_once("DB.php");
$db = new DB($db_connection);
...
init.php
//require_once("PasswordHash.php"); // Location no.2 to try it
//$password_hash = new PasswordHash(8, FALSE);
require_once("db_config.php")
..Other init stuff..
DB.php
class DB {
...
public function login() {
// global $password_hash; -> if uncommented I get an error saying it's a non-object
// Error here
$password_accepted = $password_hash->CheckPassword($p, $hp);
}
...
}
login.php
require_once("init.php");
$db->login();
I still haven't got my head fully around how class scope works in PHP so I have a feeling I'm missing something.
Upvotes: 1
Views: 462
Reputation: 7687
You need to pass the hash into the class as the class has only an internal scope.
$formData = array();
$formData['email'] = '[email protected]';
require_once("PasswordHash.php"); // Location no.1 to try it
$formData['password_hash'] = new PasswordHash(8, FALSE);
include_once("DB.php");
$db = new DB($db_connection, $formData);
and in DB.php:
class DB {
// Stores the user input form data for use within the class?
private $formData;
// Runs when the class is constructed
public function __construct($formData)
{
// When the class is constructed then store this for local/interal use
$this->$formData = $formData;
}
public function login() {
// The boolean result of of checking of an internal method
// that compares user credentials against the database information?
$password_accepted = $this->CheckPassword(
$this->formData['email'],
$this->formData['password_hash']
);
}
private function CheckPassword($email, $pass) {
// Do query and bind in $user and $pass
// Return true if everthing passes
}
}
Edit: I exaggerated the use of passing the variables into classes and methods to help you to wrap your head around this aspect of things but you could also do something like:
...
$password_accepted = $this->CheckPassword();
}
private function CheckPassword() {
// Do query and bind in $this->formData['email'] and $this->formData['password_hash']
// Return true if everthing passes
}
}
Upvotes: 2
Reputation: 96159
Just inject the hash instance the same way you already do with the db_connection.
class DB {
...
public function __construct($db_connection, $password_hash) {
// you probably already have something like
$this->connection = $db_connection;
// now do the same for the hash object
$this->pwhash = $password_hash;
}
public function login() {
...
$password_accepted = $this->pwhash->CheckPassword($p, $hp);
...
}
}
(slightly offtopic: Database and Hash ...along with Cipher, EMail and Buffer these are my least liked classes for beginners to fiddle with)
Upvotes: 2