可能使用不同类别的函数__construct? [英] Possible to use a function inside of a different classes __construct?

查看:139
本文介绍了可能使用不同类别的函数__construct?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT :::

所以我有类,我想一起工作。我的前两个建立与数据库的连接:

::
So I have classes that I would like to work together. My first two establish a connection to the database:


dbconn.php


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


dblogin.php

<?php

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

?>

我的其他类试图从数据库编辑项目。我试图通过这个类的__construct链接数据库连接类,我只是这样做所有错误显然。

editbeers.php

My other class is trying to edit items from the database. I tried to link the db connection classes throught the __construct of this class, I'm just going about this all wrong apparently.
editbeers.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;
    }


}
?>


推荐答案


  1. 您引用的第二个文件从未实际创建 $ dbconn 。如果您认为应该在 /home/carlton/public_html/PHPproject/allincludes.php 中创建,那么您应该仔细检查一下。

  1. In the second file you've quoted you never actually create $dbconn. If you think it should be created somewhere within /home/carlton/public_html/PHPproject/allincludes.php then you should probably double-check that.

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

Your constructor could check to see if the information passed to it is somehow valid before allowing it to be stored.

这篇关于可能使用不同类别的函数__construct?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆