正确的OOP PHP类结构和数据库交互
我对OOP和PHP相当陌生。我阅读了Kevin Yank的书,并希望继续向一些OOP概念迈进。但是我有点迷路了,我需要帮助 我试图使用供应商类从DB生成供应商列表。 诸如city和country之类的字段使用ID(使用city和country表的关系数据库),因此在将完整列表发布到html输出文件之前,我还需要能够获取这些字段的名称值 我可以按程序完成所有这些,但在转向OOP时,我很难找到正确的结构 我按照Kevin的指示,为我的PDO db连接创建了一个db.inc.php文件:正确的OOP PHP类结构和数据库交互,php,mysql,class,oop,pdo,Php,Mysql,Class,Oop,Pdo,我对OOP和PHP相当陌生。我阅读了Kevin Yank的书,并希望继续向一些OOP概念迈进。但是我有点迷路了,我需要帮助 我试图使用供应商类从DB生成供应商列表。 诸如city和country之类的字段使用ID(使用city和country表的关系数据库),因此在将完整列表发布到html输出文件之前,我还需要能够获取这些字段的名称值 我可以按程序完成所有这些,但在转向OOP时,我很难找到正确的结构 我按照Kevin的指示,为我的PDO db连接创建了一个db.inc.php文件: <?p
<?php
//db connection using PDO class object
try {
//create new PDO class object
$pdo = new
PDO('mysql:host=122.2.2.2;port=3307;dbname=mydbname','dbuser','password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
//if unable to connect, tell us why
$output = 'Unable to connect to the database server 2.'.$e->getMessage();
include 'output.html.php';
exit();
}
我的问题是:
非常感谢您的帮助。我认为您不应该单独创建类来获取城市名称和国家名称,只需在数据库中创建一个视图,选择供应商的详细信息,用名称替换ID,然后从中选择*。。。 为了简单起见,将所有属性放在一个数组中是一个更好的选择
只需将sql返回的数组保存到数组中…您的供应商对象应该只是一个数据容器,而不是将其紧密耦合到特定的DBMS。您将两个主题混为一谈—OOP设计和数据库抽象模式。对不起,如果这更令人困惑,但如果你不考虑同一个整体的那些部分,这将有助于你的搜索,因为它们不是。当然,你可以把它抽象出来,做一个VANDOR集合类,或者处理一组不同的供应商,但是这取决于你在做什么。你们有很多供应商吗?将它们同时显示在屏幕上是一个好的UI概念吗?一次将它们全部载入内存是个好主意吗?你有没有办法建立并返回一个供应商?你真的不应该担心“从数据库返回行”,你应该更担心创建一个API来实例化和操作对象,其中的DB部分自然来自于此祝你好运,现在PHP实际上对OOP有相当不错的支持,虽然大多数API是过程性的,就像php
array
一样,它不是一个对象,所以我总是认为php是一个OOP的烂摊子,无助于它自己的学习,真的不喜欢它,因为我离开了php,但是呃,程序员。。。你用得最多的是你得到的爱:)
<?php
class Vendor {
protected $name;
protected $address;
protected $city;
protected $state;
protected $postal;
protected $country;
protected $tel;
protected $email;
protected $web;
public function __construct(){
//typically first function to appear in a class
// Constructor using MagicMethod
$this->name = $name;
$this->address = $address;
$this->city = $city;
$this->state = $state;
$this->postal = $postal;
$this->country = $country;
$this->tel= $tel;
$this->email = $email;
$this->web = $web;
}
public function getVendors($pdo){
//get all vendor data from db
try
{
//prepares a statement for execution and returns a statement object
$query = $pdo->prepare("SELECT * FROM vendor ORDER BY name");
$query->execute();
$vendors = array();
foreach ($query->fetchAll() as $row){
//create array of member objects
$vendors[] = new Vendor($row);
}
//return member objects
//not sure this is the right thing to do here
return $vendors;
}
catch (PDOException $e)
{
//handle any errors
$output = 'Error fetching vendors: ' . $e->getMessage();
include 'output.html.php';
exit();
}//end try catch
}//end function
//setters
public function setName( $name ){
$this->name = $name;
}
public function setAddress( $address ){
$this->address = $address;
}
public function setCity( $city ){
$this->city = $city;
}
public function setState( $state ){
$this->state = $state;
}
public function setPostal( $postal ){
$this->postal = $postal;
}
public function setCountry( $country ){
$this->country = $country;
}
public function setTel( $tel ){
$this->tel = $tel;
}
public function setEmail( $email ){
$this->email = $email;
}
public function setWeb( $web ){
$this->web = $web;
}
//getters
public function getName(){
return $this->name;
}
public function getAddress(){
return $this->address;
}
public function getCity(){
return $this->city;
}
public function getState(){
return $this->state;
}
public function getPostal(){
return $this->postal;
}
public function getCountry(){
return $this->country;
}
public function getTel(){
return $this->tel;
}
public function getEmail(){
return $this->email;
}
public function getWeb(){
return $this->web;
}
}
<?php
//get all vendors
include_once $_SERVER['DOCUMENT_ROOT'] .'/db.inc.php';
include_once $_SERVER['DOCUMENT_ROOT'] .'/includes/Vendor.Class.php';
//instantiate class
$vendor = new Vendor();
//calling my function to get a list of all vendors
//not sure if passing in $pdo object is a good idea/practice
$vendors = $vendor->getVendors($pdo);
//test print out vendors
echo $vendors;
print_r($vendors);
//output to template
include 'output.html.php';
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php foreach ($vendors as $vendor){
//if my city is an id value, at what point do I call the City Class to look up
that city name? I should not be calling DB from my view. And is it OK to have
html code in my controller?
$name = $vendor->getName();
$address2 = $vendor->getAddress2();
$city = $vendor->getCity();
$zip = $vendor->getZip();
$web = $vendor->getWeb();
$phone = $vendor->getTel();
?>
</body>
</html>