doujie7497 2016-07-11 10:21
浏览 38
已采纳

无法将PDO Connection实例传递到另一个类实例

Hello I have a sample project I'm working on and I have created so far 5 php files.

connection_info.php -> db credentials
connection.php -> database connection
database.php -> where all wueries will be (here I pass the connection instance)
logger.php -> a simple logger
index.php -> oh well index...

Index.php

<?php

    require_once 'logger.php';
    require_once 'connection.php';
    require_once 'database.php';

    $logger = new Logger();
    $connection = new Connection($connection_info['host'], $connection_info['db'], $connection_info['user'], $connection_info['pass'], $logger);
    $db = new Database($connection, $logger);




?>
<!DOCTYPE html>
<html>
<head>
    <title>Administration</title>
</head>
<body>

    <?php

        $db->authenticateUser("alex", "123456");

    ?>

</body>
</html>

Connection.php

<?php

require_once 'connection_info.php';

Class Connection {

    private $user;
    private $pass;
    private $host;
    private $db;
    private $con;
    private $logger;

    function __construct($host = '127.0.0.1', $db, $user, $pass = null, &$logger) {

        $this->user = $user;
        $this->pass = $pass;
        $this->host = $host;
        $this->db = $db;
        $this->logger = $logger;

        try {

            $connectionString = "mysql:host=$this->host;dbname=$this->db;charset=utf8";

            $this->con = new PDO($connectionString, $this->user, $this->pass);
            $this->logger->log('Database connection established');

            return $this->con;

        } catch (PDOException $e) {

            $this->logger->log('Database connection failed: ' .  $e->getMessage());

        }

    }

    function __destruct() {

        $this->con = null;

    }

}

?>

Database.php

<?php

    Class Database {

        private $connection;
        private $logger;

        function __construct(&$connection, &$logger) {

            $this->connection = $connection;
            $this->logger = $logger;

        }

        function __destruct() {

        }


        public function authenticateUser($user, $pass) {

            if (func_num_args() != 2) {

                $this->logger('Invalid arguemnts supplied while authenticating');

                return false;

            } else {

                try {

                    $query = "SELECT * FROM `users` WHERE user=:user AND pass=:pass";

                    $stmt = $this->connection->prepare($query); // Error occurs here
                    $stmt->bindValue(':user', $user, PDO::PARAM_STR);
                    $stmt->bindValue(':pass', md5($pass), PDO::PARAM_STR);

                    $stmt->execute();

                    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

                    if (count($results) == 1) {

                        $_SESSION['user'] = true;

                        return true;

                    } else {

                        $this->logger('Invalid credentials provided: "' . $user . '", "' . $pass . '"' );

                        return false;

                    }

                } catch (PDOException $e) {

                    $this->logger('Error retrieving user: "' . $user . '", ' . $e->getMessage() );

                    return false;

                }

            }

        }

    }

?>

And the error I'm getting is this: "Fatal error: Call to undefined method Connection::prepare() in D:\Development\xampp\htdocs\lab\feticcio\database.php on line 34"

I'm trying to figure this out myself but my mind got stuck probably it's something silly but I could use a second pair of eyes...

  • 写回答

1条回答 默认 最新

  • douniewei6346 2016-07-11 10:28
    关注

    Your "Connection" class has no method called "prepare". You're passing an instance of Connection into Database, and then calling a method called prepare() which doesn't exist in that class. Did you mean to call the prepare() method of the PD connection object (which is $con within your own Connection class)? You should expose that via a wrapper property (or method to directly expose the $con->prepare() method).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!