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

致命错误:调用未定义的方法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

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘