<?phpclass ShopProduct{ private $id = 0; private $title; private $producerMainName; private $producerFirstName; protected $price; private $discount = 0; function __construct($title,$firstName,$mainName,$price){ $this->title = $title; $this->producerMainName = $mainName; $this->producerFirstName = $firstName; $this->price = $price; } function setDiscount($num){ $this->discount = $num; } function getDiscount(){ return $this->discount; } function getTitle(){ return $this->title; } function getProducerFirstName(){ return $this->producerFirstName; } function getProducerMainName(){ return $this->producerMainName; } function getId(){ return $this->id; } function setId($id){ $this->id = $id; } function static getInstance($id,PDO $pdo){ $stmt = $pdo->prepare("select * from products_4 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'] ); } elseif($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->getDiscount($row['discount']); return $product; } function getPrice(){ return "({$this->price} - {$this->discount})"; } function getProducer(){ return "{$this->producerFirstName}". " {$this->producerMainName}"; } function getSummaryLine(){ $base = "{$this->title} ( {$this->producerMainName}"; $base .= " {$this->producerFirstName} )"; return $base; }}class CdProduct extends ShopProduct{ private $playLength = 0; function __construct($title,$firstName,$mainName,$price,$playLength){ parent::__construct($title,$firstName,$mainName,$price); $this->playLength = $playLength; } function getSummaryLine(){ $base = parent::getSummaryLine(); $base .= ": playing - time {$this->playLength}"; return $base; }}class BookProduct extends ShopProduct{ private $numPages = 0 ; function __construct($title,$firstName,$mainName,$price,$numPages){ parent::__construct($title,$firstName,$mainName,$price); $this->numPages = $numPages; } function getNumPages(){ return $this->numPages; } function getSummaryLine(){ $base = parent::getSummaryLine(); $base .= ": page count - {$this->numPages}"; return $base; } function getPrice(){ return $this->price; }}$dsn = "mysql:host=localhost;dbname=test";try{ $pdo = new PDO($dsn,"root","root"); $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $obj = ShopProduct::getInstance(1,$pdo);}catch(PDOException $e){ echo $e->getMessage();}print_r($obj);
function static 写反了,应为
static function
function static 写反了,应为
static function
可能有吧,这是常识!
static function getInstance($id,PDO $pdo){
static 是修饰 function 的,是说名为 getInstance 的 function 是静态的
而你写成 function static getInstance($id,PDO $pdo){ 的话
且不说 static 的位置不对
function 后面应该是函数名,难不成就是 static getInstance ?
函数名也不能拆成两段呀,这不合语法
可能有吧,这是常识!
static function getInstance($id,PDO $pdo){
static 是修饰 function 的,是说名为 getInstance 的 function 是静态的
而你写成 function static getInstance($id,PDO $pdo){ 的话
且不说 static 的位置不对
function 后面应该是函数名,难不成就是 static getInstance ?
函数名也不能拆成两段呀,这不合语法
看错了,谢谢您。结账。
function static getInstance 改为 static function getInstance
static是修饰符,按语法规则需要写在被修饰的变量或方法前面。