PHP面向?qū)ο蟪绦蛟O(shè)計(jì)高級(jí)特性詳解(接口,繼承,抽象類,析構(gòu),克隆等)
本文實(shí)例講述了PHP面向?qū)ο蟪绦蛟O(shè)計(jì)高級(jí)特性。分享給大家供大家參考,具體如下:
靜態(tài)屬性
<?php
class StaticExample {
static public $aNum = 0; // 靜態(tài)共有屬性
static public function sayHello() { // 靜態(tài)共有方法
print "hello";
}
}
print StaticExample::$aNum;
StaticExample::sayHello();
?>
輸出:0 hello
點(diǎn)評(píng):靜態(tài)屬性和方法,可以通過(guò)類直接調(diào)用。
SELF
<?php
class StaticExample {
static public $aNum = 0;
static public function sayHello() { // 這里的static 和 public的順序可以顛倒
self::$aNum++;
print "hello (".self::$aNum.")\n"; // self 指向當(dāng)前類, $this指向當(dāng)前對(duì)象。
}
}
StaticExample::sayHello();
StaticExample::sayHello();
StaticExample::sayHello();
?>
輸出:
hello (1) hello (2) hello (3)
點(diǎn)評(píng):self 指向當(dāng)前類, this指向當(dāng)前對(duì)象。self可以調(diào)用當(dāng)前類的靜態(tài)屬性和方法。this指向當(dāng)前對(duì)象。self可以調(diào)用當(dāng)前類的靜態(tài)屬性和方法。this可以調(diào)用當(dāng)前類的正常屬性和方法。
常量屬性
<?php
class ShopProduct {
const AVAILABLE = 0; // 只能用大寫字母命名常量
const OUT_OF_STOCK = 1;
public $status;
}
print ShopProduct::AVAILABLE;
?>
輸出:0
點(diǎn)評(píng):常量只能用大寫字母,并且可以通過(guò)類直接調(diào)用。
接口
<?php
interface Chargeable { // 接口,抽象類是介于基類與接口之間的東西
public function getPrice();
}
class ShopProduct implements Chargeable {
// ...
protected $price;
// ...
public function getPrice() {
return $this->price;
}
// ...
}
$product = new ShopProduct();
?>
如果沒(méi)有實(shí)現(xiàn)getPrice方法,將會(huì)報(bào)錯(cuò)。
Fatal error: Class ShopProduct contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Chargeable::getPrice)
繼承類與接口
<?php
class TimedService{ }
interface Bookable{ }
interface Chargeable{ }
class Consultancy extends TimedService implements Bookable, Chargeable { // 繼承類與接口
// ...
}
?>
抽象類
先來(lái)看一段代碼
<?php
abstract class DomainObject {
}
class User extends DomainObject {
public static function create() {
return new User();
}
}
class Document extends DomainObject {
public static function create() {
return new Document();
}
}
$document = Document::create();
print_r( $document );
?>
輸出:
Document Object ( )
靜態(tài)方法
<?php
abstract class DomainObject {
private $group; // 私有屬性group
public function __construct() {
$this->group = static::getGroup();//static 靜態(tài)類
}
public static function create() {
return new static();
}
static function getGroup() { // 靜態(tài)方法
return "default";
}
}
class User extends DomainObject {
}
class Document extends DomainObject {
static function getGroup() { // 改變了內(nèi)容
return "document";
}
}
class SpreadSheet extends Document { // 繼承之后,group也就與document相同了
}
print_r(User::create());
print_r(SpreadSheet::create());
?>
輸出:
User Object ( [group:DomainObject:private] => default ) SpreadSheet Object ( [group:DomainObject:private] => document )
final字段
使類無(wú)法被繼承,用的不多
<?php
final class Checkout { // 終止類的繼承
// ...
}
class IllegalCheckout extends Checkout {
// ...
}
$checkout = new Checkout();
?>
輸出:
Fatal error: Class IllegalCheckout may not inherit from final class (Checkout)
final方法不能夠被重寫
<?php
class Checkout {
final function totalize() {
// calculate bill
}
}
class IllegalCheckout extends Checkout {
function totalize() { // 不能重寫final方法
// change bill calculation
}
}
$checkout = new Checkout();
?>
輸出:
Fatal error: Cannot override final method Checkout::totalize()
析構(gòu)函數(shù)
<?php
class Person {
protected $name;
private $age;
private $id;
function __construct( $name, $age ) {
$this->name = $name;
$this->age = $age;
}
function setId( $id ) {
$this->id = $id;
}
function __destruct() { // 析構(gòu)函數(shù)
if ( ! empty( $this->id ) ) {
// save Person data
print "saving person\n";
}
if ( empty( $this->id ) ) {
// save Person data
print "do nothing\n";
}
}
}
$person = new Person( "bob", 44 );
$person->setId( 343 );
$person->setId( '' ); // 最后執(zhí)行析構(gòu)函數(shù),使用完之后執(zhí)行
?>
輸出:
do nothing
__clone方法
克隆的時(shí)候執(zhí)行
<?php
class Person {
private $name;
private $age;
private $id;
function __construct( $name, $age ) {
$this->name = $name;
$this->age = $age;
}
function setId( $id ) {
$this->id = $id;
}
function __clone() { // 克隆時(shí)候執(zhí)行
$this->id = 0;
}
}
$person = new Person( "bob", 44 );
$person->setId( 343 );
$person2 = clone $person;
print_r( $person );
print_r( $person2 );
?>
輸出:
Person Object ( [name:Person:private] => bob [age:Person:private] => 44 [id:Person:private] => 343 ) Person Object ( [name:Person:private] => bob [age:Person:private] => 44 [id:Person:private] => 0 )
再看一個(gè)例子
<?php
class Account { // 賬戶類
public $balance; // 余額
function __construct( $balance ) {
$this->balance = $balance;
}
}
class Person {
private $name;
private $age;
private $id;
public $account;
function __construct( $name, $age, Account $account ) {
$this->name = $name;
$this->age = $age;
$this->account = $account;
}
function setId( $id ) {
$this->id = $id;
}
function __clone() {
$this->id = 0;
}
}
$person = new Person( "bob", 44, new Account( 200 ) ); // 以類對(duì)象作為參數(shù)
$person->setId( 343 );
$person2 = clone $person;
// give $person some money
$person->account->balance += 10;
// $person2 sees the credit too
print $person2->account->balance; // person的屬性account也是一個(gè)類,他的屬性balance的值是210
// output:
// 210
?>
點(diǎn)評(píng):學(xué)習(xí)還是能夠開(kāi)拓大腦的,今天終于明白為什么有多個(gè)箭頭的概念了$person->account->balance。這里的account屬性是一個(gè)對(duì)象。
__toString
<?php
class Person {
function getName() { return "Bob"; }
function getAge() { return 44; }
function __toString() {
$desc = $this->getName()." (age ";
$desc .= $this->getAge().")";
return $desc;
}
}
$person = new Person();
print $person; // 打印時(shí)候集中處理
// Bob (age 44)
?>
點(diǎn)評(píng):必須是print或echo時(shí)才有效,print_r就輸出對(duì)象。
Person Object()
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP SPL標(biāo)準(zhǔn)庫(kù)之接口(Interface)詳解
- 領(lǐng)悟php接口中interface存在的意義
- php 接口類與抽象類的實(shí)際作用
- PHP中關(guān)鍵字interface和implements詳解
- PHP abstract與interface之間的區(qū)別
- PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之接口用法
- PHP面向?qū)ο蟮倪M(jìn)階學(xué)習(xí)(抽像類、接口、final、類常量)
- PHP入門教程之面向?qū)ο蟮奶匦苑治?繼承,多態(tài),接口,抽象類,抽象方法等)
- PHP接口類(interface)的定義、特點(diǎn)和應(yīng)用示例
相關(guān)文章
采用header定義為文件然后readfile下載(隱藏下載地址)
有時(shí)候我們?yōu)榱穗[藏真實(shí)的下載地址,我們通過(guò)采用header定義為文件然后readfile下載,但這樣會(huì)加大服務(wù)器的負(fù)擔(dān),一般不建議下載量比較大的文件2014-01-01
PHP strtok()函數(shù)的優(yōu)點(diǎn)分析
相對(duì)于explode()來(lái)說(shuō),strtok()函數(shù)可以控制節(jié)奏。按需切割字串。2010-03-03
php !function_exists("T7FC56270E7A70FA81A5935B72EACBE29
今天在百度知道上面有個(gè)朋友問(wèn)php代碼解密的問(wèn)題,看了代碼不是常見(jiàn)幾種比較感興趣,特意搜索了下,發(fā)現(xiàn)下面的方法,解決了,具體的看最后的說(shuō)明。2011-01-01
php中顯示數(shù)組與對(duì)象的實(shí)現(xiàn)代碼
數(shù)組是PHP中變量的一種,在PHP開(kāi)發(fā)中經(jīng)常使用,因此非常重要,使用PHP語(yǔ)句打印數(shù)組內(nèi)容。2011-04-04
PHP登陸后跳轉(zhuǎn)到登陸前頁(yè)面實(shí)現(xiàn)思路及代碼
PHP登陸后跳轉(zhuǎn)到登陸前頁(yè)面,利用$_SERVER全局變量可以實(shí)現(xiàn)這個(gè)功能,下面有個(gè)不錯(cuò)的示例,希望對(duì)大家有所幫助2014-01-01
PHP utf-8編碼問(wèn)題,utf8編碼,數(shù)據(jù)庫(kù)亂碼,頁(yè)面顯示輸出亂碼
老聲長(zhǎng)談,著是困惑很多人的問(wèn)題,如果處理不好,都是亂碼,說(shuō)這些話并不是我對(duì)編碼很精通,只是在這方面是得留神,自己總結(jié)了一點(diǎn)小經(jīng)驗(yàn)2013-04-04

