如何在类中使用另一个类的函数?

时间:2014-05-03 22:20:49

标签: php mysql function pdo nested-class

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


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);
    }
}
?>
<html>
    <h2>
        Hold My Beer!<br />
        <meta charset="UTF-8">
        <title>Hold My Beer!</title>
    </h2>
    <body>

    </body>
</html>


dblogin.php

<?php

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

?>

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

<?php
//a couple methods of trying to get this connecting to the db...neither working.


class BeerEditor
{
    private $db;

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

    }

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

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

        // // was getting this warning "//Strict Standards: Only variables should be passed by reference in /path/to/file.php on line 123"
        // so i set them to vars
        $getbeer = $beerObj->getBeerName();
        $gettype = $beerObj->getBeerType();
        $getabv = $beerObj->getBeerABV();
        $getrating = $beerObj->getBeerRating();

        $stmt->bindParam(':beer_name', $getbeer);
        $stmt->bindParam(':beer_type', $gettype);
        $stmt->bindParam(':beer_abv', $getabv);
        $stmt->bindParam(':beer_rating', $getrating);

        $result = $stmt->execute();
        print_r($result);
        if($result === false){
            var_dump($stmt->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;
    }


}
?>


啤酒班

<?php
/**
 * Description of beer
 *
 * @author root
 */
class beer {

    public $beer_name;   //varchar(45)
    public $beer_type;   //varchar(45)
    public $beer_abv;    //decimal(4,2) alcohol percentage ex. 06.50
    public $beer_rating; //char(10) 1 awful beer, 10 life-changing beer


    public function __construct($beer_name = null){
        if ($beer_name !== null){
            $this->setBeerName($beer_name);
        }    
        //defaults
        $this->setBeerType($_POST['beer_type']);
        $this->setBeerABV($_POST['beer_abv']);
        $this->setBeerRating($_POST['beer_rating']);


    }


        public function setBeerName($beer_name){ $this->beer_name = $beer_name; }
        public function getBeerName(){
                return $this->beer_name;
        }

        public function setBeerType($beer_type){ $this->beer_name = $beer_type; }

        public function getBeerType(){
            return $this->beer_type;
        }

        public function setBeerABV($beer_abv){ $this->beer_abv = $beer_abv; }
        public function getBeerABV(){
            return $this->beer_abv;
        }

        public function setBeerRating($beer_rating){ $this->beer_rating = $beer_rating;}
        public function getBeerRating(){
            return $this->beer_rating;
        }
}


addbeer.php

<?php

session_start();

if ($_SESSION['logged_in']!="yes"){
    header ("Location: unauth.php");
    exit();
}
require_once "/home/carlton/public_html/PHPproject/allincludes.php";
?>      
<h4> So you tried a new beer..tell me about it</h4>

    <form action="beeradded.php" method="post">
        Please enter a beer:<br>
        <input name="beer_name" type="text" />
        <br>
        Type of beer:<br>
        <input type="text" name="beer_type">
        <br>
        Beer ABV: <br>
        <input type="number" name="beer_abv">
        <br>
        Rate beer from 1 to 10: <br>
        <input type="number" name="beer_rating"> 
        <br>
 <input type="submit" value='Add Beer' />
</form>

1 个答案:

答案 0 :(得分:1)

有几种方法可以让课程一起工作,但不清楚你要做什么。这里有一些普遍的帮助,也许这会让你朝着正确的方向前进。

首先,您应避免在班级中使用$_POST等超级全局。更好的方法是从顶层注入$_POST并将其视为数组。它使您的课程更加通用和灵活。

其次,BeerEditor在其构造函数中注入了数据库。这是一个最好的做法,所以在那里支持。我没有看到任何明显错误的内容,但您不包括您在BeerEditor实例化的地方。它根本不清楚beer类的用途是什么。