在另一个类函数中调用类函数的问题

时间:2014-10-21 08:38:28

标签: php function class

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

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时(将用于检查db中是否已存在用户名),我收到错误:

  

注意:未定义的变量:登录   /opt/www/decocka2/web/www.anthonydecock.be/core/conn/connection.php on   第77行

     

捕获致命错误:参数1传递给Connection :: __ construct()   必须是Log的实例,null给定,调用   /opt/www/decocka2/web/www.anthonydecock.be/core/conn/connection.php on   第77行并定义于   /opt/www/decocka2/web/www.anthonydecock.be/core/conn/connection.php on   第7行

我做错了什么?

1 个答案:

答案 0 :(得分:0)

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