2017-05-13 13 views
0

次の関数を静的に変換したいと思います。クラスのこの関数。正常に動作しています。関数を静的に変換する

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; 
       } 
     } 
} 

私はそれをフォローしました。

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; 
       } 
     } 
} 

しかし、PDOにはアクセスできません。私はPDOを使用する他の非機能を持っています。私はそれにアクセスする必要がありますか?

の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"); 
     } 

    } 

?> 

それは認証のためにと他のデータベース操作非静的メソッドのための静的メソッドを使用して良いですか?クラスを拡張するのは良いですか?pdoにgetmethodを使うべきですか?

+3

静的メソッドでオブジェクトプロパティを使用することはできません。それは目的がないからです。だからあなたは '$ this'を使うことはできません。あなたは共有プロパティまたは静的プロパティを使用することはできません。しかし、オブジェクトなしではコンストラクタは実行されません。ですから、基本的に静的メソッドは、定数を除いてクラス内のものに本当に依存できません。 – arkascha

答えて

1

静的関数で$ thisを使用することはできません。静的メソッドを静的または自己で使用できます。詳しくはドキュメントをチェックしてください。

関連する問題