Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

php - Possible to use a function inside of a different classes __construct?

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


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

?>

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;
    }


}
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...