【问题标题】:Problems calling class function in another class function在另一个类函数中调用类函数时出现问题
【发布时间】:2014-10-21 08:38:28
【问题描述】:

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

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 上 第 77 行

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

我做错了什么?

【问题讨论】:

    标签: php function class


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多