PHP對象、模式與實踐之高級特性分析
本文實例講述了PHP面向?qū)ο蟪绦蛟O(shè)計高級特性。分享給大家供大家參考,具體如下:
高級特性
包括:
1.靜態(tài)方法和屬性(通過類而不是對象來訪問數(shù)據(jù)和功能)
2.抽象類和接口(設(shè)計,實現(xiàn)分離)
3.錯誤處理(異常)
4.Final類和方法(限制繼承)
5.攔截器(自動委托)
6.析構(gòu)方法(對象銷毀前的清理工作)
7.克隆對象(創(chuàng)建對象的副本)
8.把對象解析成字符串
PS,學(xué)會從內(nèi)存的角度看代碼。想象計算機的微觀世界。
靜態(tài)方法的小例子
<?php class StaticExample{ static public $aNum = 10; static public function sayHello(){ print "hello"; } } print StaticExample::$aNum."<br/>"; StaticExample::sayHello();
tips:
1.靜態(tài)方法不能訪問類中的普通屬性,因為那些屬性屬于一個對象,但可以訪問靜態(tài)屬性。
2.我們不能再對象中調(diào)用靜態(tài)方法,因此不能再靜態(tài)方法中使用偽變量$this。
靜態(tài)方法的大例子
<?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();
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP的MVC模式實現(xiàn)原理分析(一相簡單的MVC框架范例)
- php中安全模式safe_mode配置教程
- 雞肋的PHP單例模式應(yīng)用詳解
- php設(shè)計模式 Template (模板模式)
- PHP面向?qū)ο蟪绦蛟O(shè)計高級特性詳解(接口,繼承,抽象類,析構(gòu),克隆等)
- PHP面向?qū)ο罄^承用法詳解(優(yōu)化與減少代碼重復(fù))
- PHP面向?qū)ο蟪绦蛟O(shè)計之類與反射API詳解
- PHP面向?qū)ο蟪绦蛟O(shè)計組合模式與裝飾模式詳解
- PHP 面向?qū)ο?final類與final方法
- PHP面向?qū)ο笾?深入理解static變量與方法
- php面向?qū)ο笾衧tatic靜態(tài)屬性和靜態(tài)方法的調(diào)用
- php簡單對象與數(shù)組的轉(zhuǎn)換函數(shù)代碼(php多層數(shù)組和對象的轉(zhuǎn)換)
- php對象和數(shù)組相互轉(zhuǎn)換的方法
相關(guān)文章
PHP實現(xiàn)找出數(shù)組中出現(xiàn)次數(shù)超過數(shù)組長度一半的數(shù)字算法示例
這篇文章主要介紹了PHP實現(xiàn)找出數(shù)組中出現(xiàn)次數(shù)超過數(shù)組長度一半的數(shù)字算法,涉及php數(shù)組的遍歷、統(tǒng)計、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10php創(chuàng)建基本身份認(rèn)證站點的方法詳解
本篇文章是對php創(chuàng)建基本身份認(rèn)證站點進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06將二維數(shù)組轉(zhuǎn)為一維數(shù)組的2種方法
在開發(fā)過程中,我們經(jīng)常需要將二維數(shù)組轉(zhuǎn)為一維數(shù)組,個人總結(jié)了2種方法,分享給大家2014-05-05php中0,null,empty,空,false,字符串關(guān)系的詳細(xì)介紹
本篇文章是對php中0,null,empty,空,false,字符串關(guān)系進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP中通過HTTP_USER_AGENT判斷是否為手機移動終端的函數(shù)代碼
經(jīng)常我們需要做一些判斷是否是手機訪問的時候,然后進(jìn)行自動跳轉(zhuǎn)操作,這是從一個PHP框架分離出來的一段判斷是否為手機移動終端的函數(shù),分享下2013-02-02