Ich möchte folgende Funktion in static.This Funktion in der Klasse konvertieren. Es funktioniert einwandfrei.Convert Function to Static
class LoginDBHandler extends DBConnection
{
private $loginObj;
private $table="user_login";
private $statement;
public function __construct()
{
parent::__construct();
$this->loginObj=new userLogin();
}
//Other Non-static Methods
public function authenticate($username,$password)
{
$password=password_hash($password,PASSWORD_BCRYPT,Config::$password_options);
$this->statement=$this->pdo->prepare("select * from $this->table where username=:username and password=:password and isActive=1");
$this->statement->bindParam(":username",$username);
$this->statement->bindParam(":password",$password);
$this->statement->execute();
$this->statement->fetchAll();
if($this->statement->rowCount()==1)
{
return true;
}
else
{
return false;
}
}
}
Ich versuchte es in folgender Weise.
class LoginDBHandler extends DBConnection
{
private $loginObj;
private $table="user_login";
private $statement;
public function __construct()
{
parent::__construct();
$this->loginObj=new userLogin();
}
//Other Non-static Methods
public static function authenticate($username,$password)
{
$password=password_hash($password,PASSWORD_BCRYPT,Config::$password_options);
$this->statement=self::$pdo->prepare("select * from $this->table where username=:username and password=:password and isActive=1");
$this->statement->bindParam(":username",$username);
$this->statement->bindParam(":password",$password);
$this->statement->execute();
$this->statement->fetchAll();
if($this->statement->rowCount()==1)
{
return true;
}
else
{
return false;
}
}
}
Aber PDO ist nicht zugänglich. Ich habe andere Nicht-Funktion, die auch PDO verwenden. Wie sollte ich darauf zugreifen?
DBConnection
<?php
class DBConnection
{
protected $pdo;
public function __construct()
{
$this->pdo = new PDO("mysql:host=localhost; dbname=db_inventory;","root","");
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->exec("set names utf8");
}
}
?>
Ist es eine gute statische Methoden für die Authentifizierung zu verwenden und für andere Datenbankoperation nicht statische Methoden? Ist es gut, die Klasse zu erweitern oder sollte ich getmethod für PDO verwenden?
Sie können keine Objekteigenschaften in einer statischen Methode verwenden. Das liegt daran, dass es kein Objekt gibt. Sie können also '$ this' nicht verwenden. Sie können jedoch gemeinsame oder statische Eigenschaften verwenden. Aber bedenken Sie, dass ohne Objekt kein Konstruktor ausgeführt wird. Im Grunde genommen kann eine statische Methode nicht wirklich von irgendeiner internen Klasse abhängen, außer Konstanten. – arkascha