【问题标题】:Possible to use a function inside of a different classes __construct?可以在不同的类__construct中使用函数吗?
【发布时间】:2014-06-19 18:07:38
【问题描述】:

编辑:::
所以我有一些我想一起工作的课程。我的前两个建立了与数据库的连接:


dbconn.php

<?php

class dbconn {
    protected $dbname;
    protected $dbuser;
    protected $dbpassword;
    protected $dbhost;
    protected $connection;

    public function __construct($dbhost, $dbname, $dbuser, $dbpass) 
    {
        $this->dbname = $dbname;
        $this->dbhost = $dbhost;
        $this->dbuser = $dbuser;
        $this->dbpass = $dbpass;
        $this->connect();
    }

    public function getConnection()
    {
        return $this->connection;
    }

    protected function connect()
    {
        $this->connection = new PDO("mysql:host={$this->dbhost};dbname={$this->dbname}", $this->dbuser, $this->dbpass);
    }
}
?>


dblogin.php

<?php

$db = new DBconn('localhost','phpproject','carl','pdt1848?')

?>

我的其他班级正在尝试编辑数据库中的项目。我试图通过这个类的 __construct 链接数据库连接类,我只是在做这一切显然都是错误的。
编辑啤酒.php

<?php

class BeerEditor
{
    protected $dbconn;

    function __construct($dbconn){
        $this->dbconn = $dbconn;
    }

    function addBeer(Beer $beerObj){
        //making connection to db here
        $conn = $this->dbconn->getConnection();

        $stmt = $conn->prepare("INSERT INTO beers (beer_name, beer_type, beer_abv, beer_rating) VALUES (:beer_name, :beer_type, :beer_abv, :beer_rating)");

        $stmt->bindParam(':beer_name', $beerObj->getBeerName());
        $stmt->bindParam(':beer_type', $beerObj->getBeerType());
        $stmt->bindParam(':beer_abv', $beerObj->getBeerABV());
        $stmt->bindParam(':beer_rating', $beerObj->getBeerRating());

        $result = $stmt->execute();

        if($result === false){
            var_dump($conn->errorCode());
        }

        return $result;
    }

    function listBeers(){

        $conn = $this->dbconn->getConnection();
        $result = $conn->query('SELECT * FROM beers');

        $result->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'beers');
        $beers = $result->fetchAll();

        return $beers;
    }


}
?>

【问题讨论】:

  • 错误非常具体。它说 $dbconn 没有定义。它应该来自哪里?
  • 您班级中的变量$dbconn 是您班级的私有变量。您页面中的变量$dbconn 是一个完全不同的变量。你需要给它一个值in your page 以便将它传递给你的类。您不能依赖类中的变量提供要传递给类的值以分配给类中的变量,该变量提供要传递给类的值...

标签: php function class pdo


【解决方案1】:

由于$dbconn 未定义且不是数据库连接对象,您无法调用方法getConnection()

创建 BeerEditor 时需要传入一个数据库连接对象。

$BeerEditor= new BeerEditor($dbconn);

【讨论】:

  • 我认为 $dbconn 应该是对具有 getConnection() 方法的数据库对象的引用。该类可能会创建一个 mysqli 或 pdo 连接
  • @Jacob 是的,你是对的。我将编辑原始帖子以包含我的 dbconn.php 类。我不知道如何让他们一起工作。
【解决方案2】:
  1. 在您引用的第二个文件中,您实际上从未创建 $dbconn。如果您认为应该在 /home/carlton/public_html/PHPproject/allincludes.php 内的某个位置创建它,那么您可能应该仔细检查一下。

  2. 您的构造函数可以在允许存储之前检查传递给它的信息是否有效。

【讨论】:

    【解决方案3】:

    也许 $dbconn 变量是在您所需文件的某处定义的。 试试看

    var_dump($dbconn);
    

    在require之后,看看它是否是你期望的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多