日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 高级特性,PHP对象、模式与实践之高级特性分析

發布時間:2024/4/19 php 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 高级特性,PHP对象、模式与实践之高级特性分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講述了PHP面向對象程序設計高級特性。分享給大家供大家參考,具體如下:

高級特性

包括:

1.靜態方法和屬性(通過類而不是對象來訪問數據和功能)

2.抽象類和接口(設計,實現分離)

3.錯誤處理(異常)

4.Final類和方法(限制繼承)

5.攔截器(自動委托)

6.析構方法(對象銷毀前的清理工作)

7.克隆對象(創建對象的副本)

8.把對象解析成字符串

PS,學會從內存的角度看代碼。想象計算機的微觀世界。

靜態方法的小例子

class StaticExample{

static public $aNum = 10;

static public function sayHello(){

print "hello";

}

}

print StaticExample::$aNum."
";

StaticExample::sayHello();

tips:

1.靜態方法不能訪問類中的普通屬性,因為那些屬性屬于一個對象,但可以訪問靜態屬性。

2.我們不能再對象中調用靜態方法,因此不能再靜態方法中使用偽變量$this。

靜態方法的大例子

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);//繼承父類的構造函數

$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程序設計有所幫助。

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的php 高级特性,PHP对象、模式与实践之高级特性分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。