在另一个类函数中调用类函数时出现问题


Problems calling class function in another class function

我正在尝试构建自己的小框架。现在我在将类函数调用到另一个类函数时遇到问题。

Users.php代码

class User extends Connection{
public function __construct(Connection $conn, Log $log){
    parent::__construct($log);
    $this->conn = $conn;
}
public function generatePassword(){
    $length = 12;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        $password .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $password;
}
public function generateToken(){
    $token = md5(uniqid(mt_rand(), true));
    return $token;
}
public function checkUsername($prefix, $username){
    echo "test";
}
}
$user = new User($conn, $log);
$conn = new Connection($log);

connection.php代码

<?php
class Connection{
private $log;
private $db;
public function __construct(Log $log){
    $this->log = $log;
    $this->createConnection();
}
public function createConnection(){
    $dataConnection = $this->getConnection();
    $host = $dataConnection['localhost'];
    $dbname = $dataConnection['dbname'];
    $user = $dataConnection['user'];
    $pass = $dataConnection['pass'];
    try{
        $this->db = new PDO('mysql:host='.$host.';dbname='.$dbname.'',$user,$pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->log->insertLog("installer","Trying to connect to database");
    } catch(PDOException $e){
        $this->errorHandling($e->getCode());
    }
}
public function errorHandling($error){
    switch ($error){
        case 2002:
            $this->log->insertLog("installer","Installer error code 2002: Host unknown");
            return ('Host unkown');
            break;
        case 1049:
            $this->log->insertLog("installer","Installer error code 1049: Database does not exist");
            return ('Database does not exist');
            break;
        case 1045:
            $this->log->insertLog("installer","Installer error code 1045: Could not connect to database");
            return ('Could nog connect to database');
            break;
    }
}
public function getConnection(){
    $xml = simplexml_load_file("config/config.xml");
    $dataConnection = array();
    $dataConnection['localhost'] = $xml->host;
    $dataConnection['dbname'] = $xml->dbname;
    $dataConnection['user'] = $xml->user;
    $dataConnection['pass'] = $xml->pass;
    return $dataConnection;
}
public function query($sql){
    try {
        $this->db->exec($sql);
    } catch(PDOException $e) {
        $this->errorHandling($e->getCode());
    }
}
public function select($sql){
    try{
        $result = $this->db->prepare($sql);
        $result->execute();
        return $result->fetchAll();
    } catch(PDOException $e){
        $this->errorHandling($e->getCode());
    }
}
}
$conn = new Connection($log);
$log = new Log();

Log.php代码

<?php
class Log{
public function createLog($file){
    $date = date("dmY");
    $location = 'var/log/'.$file.'-'.$date.'.log';
    if (!file_exists($location)){
        $file = fopen($location, 'w');
        $message = "/** LOG FILE CREATED **/'n";
        file_put_contents($location,$message, FILE_APPEND);
        fclose($file);
    }
    return $location;
}
public function insertLog($file,$message){
    $date = date("d-m-Y H:i:s", time());
    $location = $this->createLog($file);
    $message = $date." - ".$message."'n";
    file_put_contents($location, $message, FILE_APPEND);
}
}
$log = new Log();

现在,当我试图在user.php上调用函数checkUsername(将用于检查用户名是否已经存在于数据库中)时,我得到了错误:

注意:未定义变量:登录/opt/www/decka2/web/anthonydeck.be/core/connection.php77号线

可捕获的致命错误:参数1传递到连接::__construct()必须是Log的实例,给定null,在中调用/opt/www/decka2/web/anthonydeck.be/core/connection.php第77行,定义于/opt/www/decka2/web/anthonydeck.be/core/connection.php7号线

我做错了什么?

尝试在用户对象之前创建连接和日志对象。