doubi1910 2015-07-10 14:59
浏览 168
已采纳

致命错误:{address}中没有类活动时,无法访问self ::

I use of this mvc. (here is the documentation). Now I want to create a module for connect to database and run the query. but there is a error:

Fatal error: Cannot access self:: when no class scope is active in {address(in db.class.php)}

What is the problem ??

db.class.php (in the model folder)

<?php

class db{

/*** Declare instance ***/
private static $instance = NULL;


/*
* @return object (PDO)
*
* @access public
*/

public static function getInstance() {

if (!self::$instance) {

    function dataQuery($query, $params) {
    // what kind of query is this?
    $queryType = explode(' ', $query);

    // establish database connection
    try {
        self::$instance = new PDO('mysql:host=localhost;dbname=spy', USER, PASS);
        self::$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e) {
        echo $e->getMessage();
        $errorCode = $e->getCode();
    }

    // run query
    try {
        $queryResults = self::$instance->prepare($query);
        $queryResults->execute($params);
        if($queryResults != null && 'SELECT' == $queryType[0]) {
          $results = $queryResults->fetchAll(PDO::FETCH_ASSOC);
             return $results;
        }
        $queryResults = null; // first of the two steps to properly close
        $dbh = null; // second step tp close the connection
    }
    catch(PDOException $e) {
        $errorMsg = $e->getMessage();
        echo $errorMsg.'<br>';
    }
  }
}
return self::$instance;
}

} /*** end of class ***/

?>

init.php (in the includes )

<?php
function __autoload($class_name) {
    $filename = strtolower($class_name) . '.class.php';
    $file = __SITE_PATH . '/model/' . $filename;

    if (file_exists($file) == false)
    {
        return false;
    }
  include ($file);
}

 /*** a new registry object ***/
 $registry = new registry;

 /*** create the database registry object ***/
  $registry->db = db::getInstance();
?>

index.php: (in the controller folder)

<?php
Class indexController Extends baseController {

public function index() {

error_reporting(E_ALL);
ini_set('display_errors', 1);

define('USER', 'root');
define('PASS', '');


function findKitByMfgPrice($mfg, $price) {
    $query = "SELECT * FROM `test` WHERE `prod_name` LIKE ? AND `prod_price` > ?";
    $params = array($mfg, $price);
    $results = dataQuery($query, $params);
     return $results;
}

$mfg = '%Mobeius%';
$price = 34.95;
$kitsByMfgPrice = findKitByMfgPrice($mfg, $price);
echo '<pre>';

$welcome = $kitsByMfgPrice;


    /*** set a template variable ***/
        $this->registry->template->welcome = $welcome;
    /*** load the index template ***/
        $this->registry->template->show('index1');
}
?>
  • 写回答

1条回答 默认 最新

  • doudong7256 2015-07-10 15:27
    关注

    Your database should be used this way :
    db.php :

    class db{
    
    /*** Declare instance ***/
    private static $instance = NULL;
    
    /*
    * @return object (PDO)
    *
    * @access public
    */
    
    public static function getInstance() {
    
        if (!self::$instance) {
            // establish database connection
            try {
                self::$instance = new PDO('mysql:host=localhost;dbname=spy', USER, PASS);
                self::$instance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            catch(PDOException $e) {
                echo $e->getMessage();
                $errorCode = $e->getCode();
            }
        }
        return self::$instance;
    }
    
    public static function dataQuery($query, $params) {
        // what kind of query is this?
        $queryType = explode(' ', $query);
        // run query
        try {
            $queryResults = self::getInstance()->prepare($query);
            $queryResults->execute($params);
            if($queryResults != null && 'SELECT' == $queryType[0]) {
                $results = $queryResults->fetchAll(PDO::FETCH_ASSOC);
                return $results;
            }
            $queryResults = null; // first of the two steps to properly close
            $dbh = null; // second step tp close the connection
        }
        catch(PDOException $e) {
            $errorMsg = $e->getMessage();
            echo $errorMsg.'<br>';
        }
    
    } /*** end of class ***/
    

    and to use it, in your index :

    function findKitByMfgPrice($mfg, $price) {
        $query = "SELECT * FROM `test` WHERE `prod_name` LIKE ? AND `prod_price` > ?";
        $params = array($mfg, $price);
        $results = db::dataQuery($query, $params);
        return $results;
    }
    

    this should be a good start. But It still looks strange... the dataQuery methods is misplaced, it should not be part of the db class, it should be a local method of your index I think... Must think about it...

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?