4

我对使用 MYSQL 的 PDO 有点陌生,这是我的两个文件:

我有一个用于连接数据库的连接类:

class connection{

private $host = 'localhost';
private $dbname = 'devac';
private $username = 'root';
private $password ='';  

public $con = '';

function __construct(){

    $this->connect();   

}

function connect(){

    try{

        $this->con = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username, $this->password);
        $this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);


    }catch(PDOException $e){

        echo 'We\'re sorry but there was an error while trying to connect to the database';
        file_put_contents('connection.errors.txt', $e->getMessage().PHP_EOL,FILE_APPEND);

    }
}   
}

我有一个 account_info 类,用于从数据库中查询数据:

class account_info{


function getAccountInfo(){

    $acc_info = $this->con->prepare("SELECT * FROM account_info");
    $acc_info->execute();

    $results = $acc_info->fetchAll(PDO::FETCH_OBJ);

    foreach ($results as $key) {
        $results->owner_firstname;

    }
}       


}

我将这两个文件都包含在我的 index.php 页面中:

include_once 'classes/connection.class.php';
include_once 'classes/accountinfo.class.php';

$con = new connection();
$info = new account_info();
$info->getAccountInfo();

我只是无法让它工作我没有得到任何输出,我认为它与范围有关,但我不知道为什么要修复它,因为我是这个 PDO 和 OOP 的新手。提前致谢。

4

2 回答 2

10

解决方案 1

替换class account_info {class account_info extends connection {

代替

$con = new connection();
$info = new account_info();

$info = new account_info();

它应该可以工作。

解决方案 2(建议)

在这种情况下,我强烈建议您使用依赖注入来解决您的问题。只需将您的帐户类替换为:

class account_info {

    private $con;

    public function __construct(connection $con) {
        $this->con = $con->con;
    }

    public function getAccountInfo(){

        $acc_info = $this->con->prepare("SELECT * FROM account_info");
        $acc_info->execute();

        $results = $acc_info->fetchAll(PDO::FETCH_OBJ);

        foreach ($results as $key) {
            $results->owner_firstname;
        }
    }       

}

并像这样在 index.php 中使用它:

include_once 'classes/connection.class.php';
include_once 'classes/accountinfo.class.php';

$con = new connection();
$info = new account_info($con);
$info->getAccountInfo();

解释

作为一般的好规则:始终为函数(公共、受保护或私有)指定范围关键字。

第一个解决方案称为继承,我们基本上所做的就是使用连接类扩展帐户类,以便从连接类继承所有方法和属性并轻松使用它们。在这种情况下,您必须注意命名冲突。我建议你看一下PHP手册中的类继承。

The second solution is called dependency injection and it is a wildly encouraged design pattern that makes your classes accept other classes in their constructor in order to explicitly define the class dependency tree (in this case account depend from connection and without the connection we can't make account work).

Another, of thousands of possible solution, would be the one that someone posted below which is a design pattern called Singleton. However that patter has been reevaluated recently as anti-pattern and should not be used.

于 2012-07-16T23:46:46.757 回答
7

singleton一种常见的方法是在数据库类中使用模式。

像这样的东西:

class connection {

   private static $hInstance;

   public static function getInstance() {
     if (!(self::$hInstance instanceof self)) {
         self::$hInstance = new self();
     }

     return self::$hInstance;
   }

   /* your code */
}

然后,您可以简单地使用

$database = connection::getInstance(); 
$database->con->prepare(....)

ETC

于 2012-07-16T23:44:47.693 回答