douyiyi5284
2015-01-20 20:46
浏览 90
已采纳

致命错误:调用未定义的方法dbconnection :: prepare()

im studing php :) In first, sorry for my bad english, I try speak normaly :)

I always start writing some code with database, but all time have problems with extends. Please help me.

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 must including session and 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 where i tried connect to database

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 this class tri select information from my bd if user have a session 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;
        }
    }
}

if this posible, i need that core class includes session and database classes, and thes session class included database and core classes

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doushua7737 2015-01-20 21:00
    已采纳

    You're getting this error because you are calling prepare() on the session::db variable, this variable is of type dbconnection and so does not contain a prepare() method, you'd need to change your code to:

    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;
            }
        }
    }
    

    (I have added a second ->db in order to refer to the dbconnection::db variable which is of type PDO It may be worth rethinking the structure of your classes as, as you've discovered, the current structure can easily lead to confusion.

    Here's a suggested structure for these 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;
            }
        }
    }
    

    Main changes -Removed $db variables everywhere -Capitalised class names -Moved setup tasks, e.g., DbConnection::connect() to constructors

    已采纳该答案
    打赏 评论