【发布时间】:2014-04-24 16:31:05
【问题描述】:
我对 OOP 和 PHP 还很陌生。我一直在阅读 Kevin Yank 的书,并希望继续推进一些 OOP 概念。但是我有点迷茫,我需要帮助。
我正在尝试使用供应商类从数据库中生成供应商列表。 城市和国家等字段使用 ID(关系数据库使用城市和国家表),因此在我的 html 输出文件中发布完整列表之前,我还需要能够获取这些字段的名称值。
我可以通过程序完成所有这些,但在迁移到 OOP 时很难找到正确的结构。
我按照 Kevin 的说明为我的 PDO 数据库连接创建了一个 db.inc.php 文件:
<?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();
}
我创建了一个供应商类。该类将包含所有操作供应商的方法:getVendors()、getVendor($id)、deleteVendor($id)、updateVendor($id) 等。
<?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;
}
}
我有一个调用供应商列表的 index.php 文件
<?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';
想法是然后使用模板/输出html文件来输出数据:
<!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>
我的问题:
- 在我的供应商类中,我正在创建一个供应商对象数组 - 这是从数据库返回供应商行的正确 OOP 方式吗?
- 如果我的国家和城市是 ID,并且每个都有自己的类,那么何时何地是调用这些类并使用 getCity($id) 和 getCountry($id) 获取名称值而不是 id 的合适时间,并将它们纳入我的 foreach 语句?
非常感谢任何帮助。
【问题讨论】:
-
您的 Vendor 对象应该只是一个数据容器,并且不能将它与特定的 DBMS 紧密耦合。
-
您将两个主题混为一谈——OOP 设计和数据库抽象模式。很抱歉,如果这更令人困惑,但如果您不考虑同一整体的这些部分,它将有助于您的搜索,因为它们不是。
-
当然,您可以将其进一步抽象出来并制作一个 VendorCollection 类或其他东西来以不同方式处理供应商组,但这实际上取决于您在做什么。你有很多供应商吗?同时在屏幕上显示它们是否是一个好的 UI 概念?一次将它们全部加载到内存中是个好主意吗?你有建立和返回单一供应商的方法吗?你真的不应该担心“从数据库返回行”,你应该更多地担心创建一个 API 来实例化和操作对象,它的数据库部分将自然而然地来自于此
-
祝你好运,现在 PHP 实际上对 OOP 的支持相当不错,尽管如此,大部分 API 都是程序性的,比如 php
array不是对象,所以我一直认为 PHP 是一个OOP 混乱,无助于自己的学习,自从我离开 php 后,我真的不喜欢它,但是,程序员……你用得最多的就是你会喜欢的 :)