《PHP學(xué)習(xí):PHP對(duì)象、模式與實(shí)踐之高級(jí)特性分析》要點(diǎn):
本文介紹了PHP學(xué)習(xí):PHP對(duì)象、模式與實(shí)踐之高級(jí)特性分析,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP實(shí)戰(zhàn)本文實(shí)例講述了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)高級(jí)特性.分享給大家供大家參考,具體如下:
PHP實(shí)戰(zhàn)高級(jí)特性
PHP實(shí)戰(zhàn)包括:
PHP實(shí)戰(zhàn)1.靜態(tài)方法和屬性(通過(guò)類而不是對(duì)象來(lái)訪問(wèn)數(shù)據(jù)和功能)
2.抽象類和接口(設(shè)計(jì),實(shí)現(xiàn)分離)
3.錯(cuò)誤處理(異常)
4.Final類和方法(限制繼承)
5.攔截器(自動(dòng)委托)
6.析構(gòu)方法(對(duì)象銷毀前的清理工作)
7.克隆對(duì)象(創(chuàng)建對(duì)象的副本)
8.把對(duì)象解析成字符串
PHP實(shí)戰(zhàn)PS,學(xué)會(huì)從內(nèi)存的角度看代碼.想象計(jì)算機(jī)的微觀世界.
PHP實(shí)戰(zhàn)靜態(tài)方法的小例子
PHP實(shí)戰(zhàn)
<?php
class StaticExample{
static public $aNum = 10;
static public function sayHello(){
print "hello";
}
}
print StaticExample::$aNum."<br/>";
StaticExample::sayHello();
PHP實(shí)戰(zhàn)tips:
PHP實(shí)戰(zhàn)1.靜態(tài)方法不能訪問(wèn)類中的普通屬性,因?yàn)槟切傩詫儆谝粋€(gè)對(duì)象,但可以訪問(wèn)靜態(tài)屬性.
2.我們不能再對(duì)象中調(diào)用靜態(tài)方法,因此不能再靜態(tài)方法中使用偽變量$this.
PHP實(shí)戰(zhàn)靜態(tài)方法的大例子
PHP實(shí)戰(zhàn)
<?php
class ShopProduct{
private $title;
private $producerMainName;
private $producerFirstName;
protected $price;
private $discount = 0;
private $id = 0;
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 static function getInstance($id,PDO $pdo){
$query = "select * from products where id= '$id'";
$stmt = $pdo->query($query);
$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;
}
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);
}
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;
function __construct($title,$firstName,$mainName,$price,$playLength){
parent::__construct($title,$firstName,$mainName,$price);//繼承父類的構(gòu)造函數(shù)
$this->playLength = $playLength;
}
function getPlayLength(){
return $this->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;
}
}
$dsn = "sqlite:C:/Users/Administrator/Desktop/shop.db";
$pdo = new PDO($dsn,null,null);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$obj = ShopProduct::getInstance(1,$pdo);
echo $obj->getSummaryLine();
PHP實(shí)戰(zhàn)更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
PHP實(shí)戰(zhàn)希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助.
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/2469.html