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

时间:2021-02-12 11:25:54

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:

我使用这个mvc。 (这是文档)。现在我想创建一个连接数据库并运行查询的模块。但是有一个错误:

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

致命错误:当{address(在db.class.php)中没有活动类作用域时,无法访问self ::

What is the problem ??

问题是什么 ??

db.class.php (in the model folder)

db.class.php(在模型文件夹中)

<?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 )

init.php(在包含中)

<?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)

index.php :(在控制器文件夹中)

<?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 个解决方案

#1


0  

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

您的数据库应该以这种方式使用: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...

这应该是一个好的开始。但它仍然看起来很奇怪... dataQuery方法是错位的,它不应该是db类的一部分,它应该是你的索引的本地方法我认为......必须考虑一下......

#1


0  

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

您的数据库应该以这种方式使用: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...

这应该是一个好的开始。但它仍然看起来很奇怪... dataQuery方法是错位的,它不应该是db类的一部分,它应该是你的索引的本地方法我认为......必须考虑一下......