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

致命错误:{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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c