OO PHP构造函数不工作?

时间:2014-06-14 09:19:38

标签: php oop object constructor

我是开发面向对象的PHP并尝试创建登录系统的新手。因此,我制作了一个索引文件和两个类。在我看来,使用var_dump(),输出将包含元素状态,用户名等。

但是我得到了这个通知:

notice: Undefined variable: connector in C:\xampp\htdocs\1-4-LT3\Concept\Authenticator.php on line 15

Fatal error: Call to a member function connect() on a non-object in C:\xampp\htdocs\1-4-LT3\Concept\Authenticator.php on line 15

对我而言,似乎构造者并没有工作。

我做错了什么?为什么$ connector变量未定义?

哦,我知道这种方式非常不安全,但我只想尝试一下OO PHP。

index.php文件:

<?php
include 'Connector.php';
include 'Authenticator.php';
$connector = new Connector('localhost', 'root', '', 'hondsrug_db');
$authenticator = new Authenticator($connector);

$authenticator->login('root', 'admin');
var_dump($authenticator);
?>

connector.php文件:

<?php
class Connector {
    var $mysqli;
    var $host;
    var $username;
    var $password;
    var $database;
    public function Connector($host, $username, $password, $database){
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;
    }

    public function query($query){
        $result = $mysqli->query($query);
        if($result->num_rows > 0){
            return $result->fetch_assoc;
        } else {
            return null;
        }
    }
    public function connect(){
        $this->mysqli = new mysqli($host, $username, $password, $database);
    }
    public function disconnect(){
        $mysqli->close();
    }

}


?>

和Authenticator.php文件:

<?php
// maakt verbinding met db
// logt in adh van methode
// bepaalt rang, en implementeert hiervoor een getter
class Authenticator {
    var $connector;
    var $status;
    var $username;
    var $rank;
    public function Authenticator($connector){
        $this->connector = $connector;
    }

    public function login($username, $password){
        $connector->connect();
        $result = $connector->query('SELECT password, rank FROM users WHERE username = '.$username);
        if($result != null){
            if($result['password'] == SHA1($password)){
                $this->status = true;
                $this->username = $username;
                $this->rank = $result['rank'];
            } else {
                $this->status = false;
                $this->username = null;
                $this->rank = null;
            }
        } else {
            $status = false;
        }

        $connector->disconnect();
    }

    public function getRank(){
        return $rank;
    }

    public function getStatus(){
        return $status;
    }
}



?>

3 个答案:

答案 0 :(得分:1)

问题在这里

$connector->connect();

您在类Authenticator中使用了var $ connector,并且在构造函数中使用了

 $this->connector = $connector;

因此,在访问该方法时,您需要执行

 $this->connector->connect();

现在您使用类名作为函数名称使其成为构造函数,但它作为向后兼容性工作,但最好使用魔术方法__construct而不是var开始使用public,private, protected

答案 1 :(得分:0)

你必须在PHP中显式使用$ this。总是很遗憾。

public function login($username, $password){
    $this-> connector->connect();

答案 2 :(得分:0)

您的登录方法需要使用

$this->connector->connect();
$result = $this->connector->query('SELECT password, rank FROM users WHERE username = '.$username);

$this->connector->disconnect();

不是

$connector->connect();
$result = $connector->query('SELECT password, rank FROM users WHERE username = '.$username);

$connector->disconnect();