Databae.class.php →
<?php
/**
* Database class
*
*/
class Database {
private static $_db; //singleton connection object
private function __construct() {} //Disallow creating a new object of the class with new Database
private function __clone() {} //disallow cloning the class
/**
* Get the instance of the PDO connection
*
* @return DB PDO connection
*/
public static function getInstance() {
if(static::$_db === NULL) {
$dsn = 'mysql:host=' . Config::DB_HOST . ';dbname=' . Config::DB_NAME . ';charset=utf8';
static::$_db = new PDO($dsn, Config::DB_USER, Config::DB_PASS);
//Raise exceptions when a database exception occurs
static::$_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return static::$_db;
}
}
User.class.php →
<?php
/**
* User class
*/
class User
{
public $errors;
/**
* Signup a new user
*
* @param array $data POST data
* @return User
*/
public static function signup($data)
{
// Create a new user model and set the attributes
$user = new static();
$user->name = $data['name'];
$user->email = $data['email'];
$user->password = $data['password'];
if ($user->isValid()) {
try {
$db = Database::getInstance();
$stmt = $db->prepare('INSERT INTO users (name, email, password) VALUES (:name, :email, :password)');
$stmt->bindParam(':name', $user->name);
$stmt->bindParam(':email', $user->email);
$stmt->bindParam(':password', Hash::make($user->password));
$stmt->execute();
} catch(PDOException $exception) {
// Log the exception message
error_log($exception->getMessage());
}
}
return $user;
}
/**
* See if an user record already exists with the specified email address
*
* @param string $email email address
* @return boolean
*/
public function emailExists($email) {
try {
$db = Database::getInstance();
$stmt = $db->prepare('SELECT COUNT(*) FROM users WHERE email = :email LIMIT 1');
$stmt->execute([':email' => $this->email]);
$rowCount = $stmt->fetchColumn();
return $rowCount == 1;
} catch(PDOException $exception) {
error_log($exception->getMessage());
return false;
}
}
/**
* Validate the properties and set $this->errors if any are invalid
*
* @return boolean true if valid, false otherwise
*/
public function isValid()
{
$this->errors = [];
//
// name
//
if ($this->name == '') {
$this->errors['name'] = 'Please enter a valid name';
}
//
// email address
//
if (filter_var($this->email, FILTER_VALIDATE_EMAIL) === false) {
$this->errors['email'] = 'Please enter a valid email address';
}
if ($this->emailExists($this->email)) {
$this->errors['email'] = 'That email address is already taken';
}
//
// password
//
if (strlen($this->password) < 5) {
$this->errors['password'] = 'Please enter a longer password';
}
return empty($this->errors);
}
}
User.class.php →
<?php
/**
* User class
*/
class User
{
public $errors;
/**
* Signup a new user
*
* @param array $data POST data
* @return User
*/
public static function signup($data)
{
// Create a new user model and set the attributes
$user = new static();
$user->name = $data['name'];
$user->email = $data['email'];
$user->password = $data['password'];
if ($user->isValid()) {
try {
$db = Database::getInstance();
$stmt = $db->prepare('INSERT INTO users (name, email, password) VALUES (:name, :email, :password)');
$stmt->bindParam(':name', $user->name);
$stmt->bindParam(':email', $user->email);
$stmt->bindParam(':password', Hash::make($user->password));
$stmt->execute();
} catch(PDOException $exception) {
// Log the exception message
error_log($exception->getMessage());
}
}
return $user;
}
/**
* See if an user record already exists with the specified email address
*
* @param string $email email address
* @return boolean
*/
public function emailExists($email) {
try {
$db = Database::getInstance();
$stmt = $db->prepare('SELECT COUNT(*) FROM users WHERE email = :email LIMIT 1');
$stmt->execute([':email' => $this->email]);
$rowCount = $stmt->fetchColumn();
return $rowCount == 1;
} catch(PDOException $exception) {
error_log($exception->getMessage());
return false;
}
}
/**
* Validate the properties and set $this->errors if any are invalid
*
* @return boolean true if valid, false otherwise
*/
public function isValid()
{
$this->errors = [];
//
// name
//
if ($this->name == '') {
$this->errors['name'] = 'Please enter a valid name';
}
//
// email address
//
if (filter_var($this->email, FILTER_VALIDATE_EMAIL) === false) {
$this->errors['email'] = 'Please enter a valid email address';
}
if ($this->emailExists($this->email)) {
$this->errors['email'] = 'That email address is already taken';
}
//
// password
//
if (strlen($this->password) < 5) {
$this->errors['password'] = 'Please enter a longer password';
}
return empty($this->errors);
}
}
$user = new static();
The above static should be class(That’s what I know that class can be instantiated as an object), but this seems to be instantiating a property _db?
Can a property be instantiated just like a class?
static
lass is not defined anywhere?