<?php
class ShopProduct {
private $title;
private $producerMainName;
private $producerFirstName;
protected $price;
private $discount = 0;
private $id = 0;
public function __construct( $title, $firstName,
$mainName, $price ) {
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mainName;
$this->price = $price;
}
public function setID( $id ) {
$this->id = $id;
}
public function getProducerFirstName() {
return $this->producerFirstName;
}
public function getProducerMainName() {
return $this->producerMainName;
}
public function setDiscount( $num ) {
$this->discount=$num;
}
public function getDiscount() {
return $this->discount;
}
public function getTitle() {
return $this->title;
}
public function getPrice() {
return ($this->price - $this->discount);
}
public function getProducer() {
return "{$this->producerFirstName}".
" {$this->producerMainName}";
}
function getSummaryLine() {
$base = "$this->title ( $this->producerMainName, ";
$base .= "$this->producerFirstName )";
return $base;
}
public static function getInstance( $id, PDO $pdo ) {
$query = "select * from products where id='$id'";
$stmt = $pdo->prepare("select * from products where id=?");
$result = $stmt->execute( array( $id ) );
$row = $stmt->fetch( );
if ( empty( $row ) ) { return null; }
if ( $row['type'] == "book" ) {
$product = new BookProduct(
$row['title'],
$row['firstname'], $row['mainname'],
$row['price'], $row['numpages'] );
} else if ( $row['type'] == "cd" ) {
$product = new CdProduct(
$row['title'],
$row['firstname'], $row['mainname'],
$row['price'], $row['playlength'] );
} else {
$product = new ShopProduct(
$row['title'],
$row['firstname'], $row['mainname'],
$row['price'] );
}
$product->setId( $row['id'] );
$product->setDiscount( $row['discount'] );
return $product;
}
}
class CdProduct extends ShopProduct {
private $playLength = 0;
public function __construct( $title, $firstName,
$mainName, $price, $playLength ) {
parent::__construct( $title, $firstName,
$mainName, $price );
$this->playLength = $playLength;
}
public function getPlayLength() {
return $this->playLength;
}
function getSummaryLine() {
$base = parent::getSummaryLine();
$base .= ": playing time - $this->playLength";
return $base;
}
}
class BookProduct extends ShopProduct {
private $numPages = 0;
public function __construct( $title, $firstName,
$mainName, $price, $numPages ) {
parent::__construct( $title, $firstName,
$mainName, $price );
$this->numPages = $numPages;
}
public function getNumberOfPages() {
return $this->numPages;
}
function getSummaryLine() {
$base = parent::getSummaryLine();
$base .= ": page count - $this->numPages";
return $base;
}
public function getPrice() {
return $this->price;
}
}
require_once("generate_product_pdo.php");
$pdo = getPDO();
$obj = ShopProduct::getInstance( 1, $pdo );
print_r( $obj );
$obj = ShopProduct::getInstance( 2, $pdo );
print_r( $obj );
$obj = ShopProduct::getInstance( 3, $pdo );
print_r( $obj );
?>
这个方法再类中会比在对象中更有用。
我们可以轻松地将原始数据转换为一个对象,而不需要一开始就使用ShopProduct对象。
这个方法并没有使用任何实例属性或方法吗所以没有理由不把他定义为static。
只要有一个有效的PDO对象,我们就可以再程序的任何地方(应该要先把class include进来吧)调用这个方法:
$dsn = "xxxxxxxxxxxxx";
$pdo = new PDO($dns,null,null);
$pdo->getAttribute(xxxxxxxxxxxxxxxxx, xxxxxxxxxxxxx);
$bjg = ShopProduct::getInstance(1, $pdo);
如果这个类的父类已经有了数据库连接的实例应该不用传这个pdo实例就可以直接返回数据了。