Fatal error: Call to undefined method dbconnection::prepare()

Fatal error: Call to undefined method dbconnection::prepare()

我正在学习 php :) 首先,抱歉我的英语不好,我试着说正常:)

我总是开始用数据库编写一些代码,但总是遇到扩展问题。请帮助我。

index.php

define('CWM', TRUE);
define('DS', DIRECTORY_SEPARATOR);
define('PATH', dirname(__FILE__) . DS);
define('LINK', dirname($_SERVER['SCRIPT_NAME']));

require_once 'classes' . DS . 'database.php';
require_once 'classes' . DS . 'session.php';
require_once 'classes' . DS . 'core.php';
$core = new core;

core.php 必须包括 session 和 dbconnection class

if(!defined('CWM')) die('script access error');
class core extends session{
    protected $db;

    function __construct(){
        $this->db = new dbconnection();
        parent::session();
    }
}

database.php class 我尝试连接到数据库的地方

class dbconnection{
protected $db;
protected $dbinfo = array();

public function connect(){
        if(file_exists(PATH . 'classes' . DS . 'config.php')){
            $this->dbinfo = require_once PATH . 'classes' . DS . 'config.php';

            try{
                $this->db = new PDO('mysql:host=' . $this->dbinfo['hostname'] . ';dbname='. $this->dbinfo['dbname'], $this->dbinfo['username'], $this->dbinfo['password'], array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
                return $this->db;
            }catch(PDOException $e){
                die($e->getMessage());
            }
        }else{
            trigger_error('undefined config.php', E_USER_ERROR);
        }
    }

    function __destruct(){
        $this->db = NULL;
    }
}

session.php 这个 class 三 select 来自我的 bd 的信息如果用户有一个会话 cookie

if(!defined('CWM')) die('script access error');
class session extends dbconnection{
    protected $db;
    protected $member = array();

    function __construct(){
        parent::connect();
        $this->session;
    }

    protected function session(){
        $_COOKIE['session'] = 5;
        if(!empty($_COOKIE['session'])){
            $this->member = $this->db->prepare("SELECT * FROM `users` WHERE `session` = '?'")->execute(array($_COOKIE['session']));
            var_dump($this->member);
        }else{
            $this->member = false;
        }
    }
}

如果可能的话,我需要核心 class 包括会话和数据库 classes,而会话 class 包括数据库和核心 classes

您收到此错误是因为您在 session::db 变量上调用 prepare(),此变量的类型为 dbconnection,因此不包含prepare() 方法,您需要将代码更改为:

if(!defined('CWM')) die('script access error');
class session extends dbconnection{
    protected $db;
    protected $member = array();

    function __construct(){
        parent::connect();
        $this->session;
    }

    protected function session(){
        $_COOKIE['session'] = 5;
        if(!empty($_COOKIE['session'])){
            $this->member = $this->db->db->prepare("SELECT * FROM `users` WHERE `session` = '?'")->execute(array($_COOKIE['session']));
            var_dump($this->member);
        }else{
            $this->member = false;
        }
    }
}

(我添加了第二个 ->db 以引用 PDO 类型的 dbconnection::db 变量 可能值得重新考虑 classes 的结构,因为正如您发现的那样,当前的结构很容易导致混淆。

下面是这些 classes 的建议结构:

Core.php

if(!defined('CWM')) die('script access error');
class Core extends Session {

    function __construct(){
        parent::__construct();
        $this->session();
    }
}

DbConnection.php

class DbConnection{
    protected $db;
    protected $dbinfo = array();

    public function __construct() {
        $this->connect();
    }    

    public function connect(){
        if(file_exists(PATH . 'classes' . DS . 'config.php')){
            $this->dbinfo = require_once PATH . 'classes' . DS . 'config.php';

            try{
                $this->db = new PDO('mysql:host=' . $this->dbinfo['hostname'] . ';dbname='. $this->dbinfo['dbname'], $this->dbinfo['username'], $this->dbinfo['password'], array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
                return $this->db;
            }catch(PDOException $e){
               die($e->getMessage());
           }
        }else{
            trigger_error('undefined config.php', E_USER_ERROR);
        }
    }

    function __destruct(){
        $this->db = NULL;
    }
}

Session.php

if(!defined('CWM')) die('script access error');
class Session extends DbConnection {

    protected $member = array();

    function __construct(){
        parent::__construct();
    }

    protected function session(){
        $_COOKIE['session'] = 5;
        if(!empty($_COOKIE['session'])){
            $this->member = $this->db->prepare("SELECT * FROM `users` WHERE `session` = '?'")->execute(array($_COOKIE['session']));
            var_dump($this->member);
        }else{
            $this->member = false;
        }
    }
}

主要变化 - 到处都删除了 $db 变量 -大写 class 个名字 - 将设置任务,例如 DbConnection::connect() 移动到构造函数