So I was looking over this:
http://stackoverflow.com/questions/11658532/php-construct
I thought ok, I get it so I went to try it myself
<?php
require "loadclasses.php";
class Login
{
private
$email,
$password,
$database;
private static
$emptyInput = 0,
$emaillogin;
public function __construct()
{
$this->database = new Database();
}
public static function validateEmptyEmail($email)
{
return(strlen($email)) > self::$emptyInput;
}
public static function validateEmptyPassword($password)
{
return(strlen($password)) > self::$emptyInput;
}
public static function checkActive($email)
{
$database->query('select eMail,active from username WHERE eMail = :emaillogin AND active = 0');
$database->execute();
$database->rowCount();
$database->rowCount();
if ($database->rowCount() === 1)
{
return $emaillogin;
}
}
}
<?php
date_default_timezone_set("Europe/London");
error_reporting(-1); // reports all errors
ini_set("display_errors", "1"); // shows all errors
ini_set("log_errors", 1);
ini_set("error_log", "php-error.log");
class Database
{
private $host = "localhost";
private $user = "root";
private $pass = "";
private $dbname = "got";
private $dbh;
private $error;
private $stmt;
public function __construct()
{
//SET DSN
$dsn = "mysql:host=". $this->host . ";dbname=" . $this->dbname;
$options = array
(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE =>PDO::ERRMODE_EXCEPTION
);
//create PDO
try
{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
catch(PDOEception $e)
{
$this->error = $e->getMessage();
}
}
public function query($query)
{
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null)
{
if(is_null($type))
{
switch(true)
{
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute()
{
return $this->stmt->execute();
}
public function lastInsertId()
{
$this->dbh->lastInsertId();
}
public function rowCount()
{
$this->stmt->rowCount();
}
public function connect()
{
$this->dbh->connect();
}
public function resultset()
{
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
<?php
error_reporting(-1); // reports all errors
ini_set("display_errors", "1"); // shows all errors
ini_set("log_errors", 1);
ini_set("error_log", "register.log");
require "loadclasses.php";
$database = new Login;
session_start();
$post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$emaillogin = $passwordlogin = "";
$emaillogin = $post["emaillogin"];
$passwordlogin = $post["passwordlogin"];
$errors = array();
$fields = array(
'emaillogin' => array(
'validator' => 'validateEmptyEmail',
'message' => 'Email is required',
),
'passwordlogin' => array(
'validator' => 'validateEmptyPassword',
'message' => 'Password is required'
),
'passwordlogin' => array(
'validator' => 'checkActive',
'message' => ' is required'
)
);
foreach($post as $key => $value)
{
if(isset($fields[$key]))
{
if(!Login::{$fields[$key]['validator']}($value))
{
$errors[] = ['name' => $key, 'error' => $fields[$key]['message']];
}
}
}
header('Content-Type: application/json');
if (empty($errors))
{
//echo json_encode($success);
}
else
{
echo json_encode(["errors" => $errors]);
}
Error message
<br />
<b>Notice</b>: Undefined variable: database in <b>E:\EasyPHP-Devserver-16.1\eds-www\gotsocial\classes\Login.class.php</b> on line <b>33</b><br />
<br />
<b>Fatal error</b>: Uncaught Error: Call to a member function query() on unknown in E:\EasyPHP-Devserver-16.1\eds-www\gotsocial\classes\Login.class.php:33
Stack trace:
#0 E:\EasyPHP-Devserver-16.1\eds-www\gotsocial\loginForm.php(40): Login::checkActive('kkkkkkk')
#1 {main}
thrown in <b>E:\EasyPHP-Devserver-16.1\eds-www\gotsocial\classes\Login.class.php</b> on line <b>33</b><br />