范围错误 - 在非对象上调用成员函数prepare()

时间:2011-02-06 00:57:00

标签: php pdo

所以场景很简单。我使用在数据库中执行某些操作的类,但在该类中,我调用另一个也在DB中执行某些操作的类。

谢谢,include_once已更改为include并且有效!

这就是我得到的:

  

致命错误:调用成员函数   prepare()on non-object - >第20行 mLog.php

我使用db_config.php创建PDO对象,然后将其包含在我的类中。

db_config.php

try
{
    $DBH = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);

    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch (PDOException $e)
{
    echo $e->getMessage();
}

第一课 mLog.php

<?php

    class Log
    {
        public static function Add($action)
        {
            try
            {
                include_once "db_config.php";

                $ip = $_SERVER['REMOTE_ADDR'];

                $time = date('Y-m-d');

                $values = array($ip, $action, $time);
//ERROR NEXT LINE
                $STH = $DBH->prepare("INSERT INTO log (ip, action, time)
                                      VALUES (?, ?, ?)");

                $STH->execute($values);

                $DBH = null;
                $STH = null;
            }
            catch (PDOException $e)
            {
                echo $e->getMessage();
            }
        }
    }

使用第一类的第二类(片段因为它很大而且功能很多)

public static function Add($catName, $catDescr = "", $catImgURL = "", $catSubLevel = 0, $catSubID = 0)
{
    try
    {
        include_once "db_config.php";
        include_once "mLog.php";

        $values = array($catName, $catDescr, $catImgURL, $catSubLevel, $catSubID);
        $STH = $DBH->prepare("INSERT INTO cat (catName, catDescr, catImg, catSubLevel, catSubID)
                              VALUES (?, ?, ?, ?, ?)");

        $STH->execute($values);

        $DBH = null;
        $STH = null;

        //HERE IT IS
        Log::Add("Added category 111" . $catName);

        return true;
    }
    catch (PDOException $e)
    {
        echo $e->getMessage();
    }
}

2 个答案:

答案 0 :(得分:1)

您使用include_once "db_config.php";代替include "db_config.php";

据我所知,每次包含db_config.php时,您都会创建数据库对象$DBH

由于你将它设置为include_once,它只会运行db_config.php一次,当你尝试包含它时,它会在日志类中运行,因为它已经包含在Add方法中了

为了改进这一点,您应该创建一个仅管理(或封装)PDO对象的类。您可以简单地创建一个返回PDO对象的Singleton类,在顶部包含一次类,然后在代码中的任何位置获取对象。

示例:

DBAccess.php

class DBAccess extends Singleton{

    // there is a getInstance() method in the Singleton abstract class

    private $dbh;

    // The PDO object is created only once when the first getInstance() is called in Singleton.
    function __construct(){
        try
        {
            $this->dbh = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);

            $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        }
        catch (PDOException $e)
        {
            echo $e->getMessage();
        }

    }

    /**
     * Get the PDO object
     * @return object
     */
    public static function getDBH(){
        return self::getInstance()->dbh;
    }

}

日志类:

class Log
{
    public static function Add($action)
    {
        try
        {
            $DBH = DBAccess::getDBH();

            $ip = $_SERVER['REMOTE_ADDR'];

            $time = date('Y-m-d');

            $values = array($ip, $action, $time);

            $STH = $DBH->prepare("INSERT INTO log (ip, action, time)
                                  VALUES (?, ?, ?)");

            $STH->execute($values);
        }
        catch (PDOException $e)
        {
            echo $e->getMessage();
        }
    }
}

用法:

include_once( 'db_config.php'); include_once( 'mLog.php');

public static function Add($catName, $catDescr = '', $catImgURL = '', $catSubLevel = 0, $catSubID = 0)
{
    try
    {

        $DBH = DBAccess::getDBH();

        $values = array($catName, $catDescr, $catImgURL, $catSubLevel, $catSubID);

        $STH = $DBH->prepare("INSERT INTO cat (catName, catDescr, catImg, catSubLevel, catSubID)
                              VALUES (?, ?, ?, ?, ?)");

        $STH->execute($values);

        $DBH = null;

        Log::Add("Added category 111" . $catName);

        return true;
    }
    catch (PDOException $e)
    {
        echo $e->getMessage();
    }
}

答案 1 :(得分:0)

$DB的范围不在类中,因为您尚未将其传递到类中。目前它只是在全球范围内浮动,但不在你的班级范围内。

您需要将$DB添加到Log课程中,您可以在db_config.php

中将此作为静态变量添加到Log::$DB = $DB;
class Log
{
    public static $DB;

    public static function Add($action)
    {
        try
        {
            include_once "db_config.php";

            $ip = $_SERVER['REMOTE_ADDR'];

            $time = date('Y-m-d');

            $values = array($ip, $action, $time);
            $STH = self::$DBH->prepare("INSERT INTO log (ip, action, time)
                                  VALUES (?, ?, ?)");

            $STH->execute($values);

            self::$DBH = null;
            $STH = null;
        }
        catch (PDOException $e)
        {
            echo $e->getMessage();
        }
    }
}

在你的班级中使用这样的

{{1}}