2014-05-03 42 views
-1

所以我有我想一起工作的课程。我的前两个建立与数据库的连接:如何在类中使用另一个类的函数?


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

有几种方法可以使类一起工作,但目前还不清楚你在做什么试图去做。这里有一些一般的帮助,也许这会让你朝着正确的方向前进。

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

其次,BeerEditor已将数据库注入到其构造函数中。这是最佳做法,因此道具在那里。我没有看到任何明显的错误,但是你没有包括你在哪里实例化BeerEditor。目前还不清楚beer类的用途。

+0

我的意图是将输入的数据发送到啤酒班;然后让啤酒编辑从啤酒类中获取信息并将其推送到数据库。 – user3598266

相关问题