這篇文章主要介紹了PHP物件導向程式設計進階特性,結合執行個體形式分析了php物件導向程式設計中所涉及的靜態屬性、常量屬性、介面、繼承、抽象類別、析構、複製等概念與提示,需要的朋友可以參考下
1. 靜態屬性
<?phpclass StaticExample { static public $aNum = 0; // 靜態共有屬性 static public function sayHello() { // 靜態共有方法 print "hello"; }}print StaticExample::$aNum;StaticExample::sayHello();?>
輸出:0 hello
點評:靜態屬性和方法,可以通過類直接調用。
2. SELF
<?phpclass StaticExample { static public $aNum = 0; static public function sayHello() { // 這裡的static 和 public的順序可以顛倒 self::$aNum++; print "hello (".self::$aNum.")\n"; // self 指向當前類, $this指向當前對象。 }}StaticExample::sayHello();StaticExample::sayHello();StaticExample::sayHello();?>
輸出:
hello (1)hello (2)hello (3)
點評:self 指向當前類, this指向當前對象。self可以調用當前類的靜態屬性和方法。this指向當前對象。self可以調用當前類的靜態屬性和方法。this可以調用當前類的正常屬性和方法。
3. 常量屬性
<?phpclass ShopProduct { const AVAILABLE = 0; // 只能用大寫字母命名常量 const OUT_OF_STOCK = 1; public $status;}print ShopProduct::AVAILABLE;?>
輸出:0
點評:常量只能用大寫字母,並且可以通過類直接調用。
4. 介面
<?phpinterface Chargeable { // 介面,抽象類別是介於基類與介面之間的東西 public function getPrice();}class ShopProduct implements Chargeable { // ... protected $price; // ... public function getPrice() { return $this->price; } // ...}$product = new ShopProduct();?>
如果沒有實現getPrice方法,將會報錯。
Fatal error: Class ShopProduct contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Chargeable::getPrice)
5. 繼承類與介面
<?phpclass TimedService{ }interface Bookable{ }interface Chargeable{ }class Consultancy extends TimedService implements Bookable, Chargeable { // 繼承類與介面 // ...}?>
6. 抽象類別
先來看一段代碼
<?phpabstract 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()
7. 靜態方法
<?phpabstract class DomainObject { private $group; // 私人屬性group public function construct() { $this->group = static::getGroup();//static 靜態類 } public static function create() { return new static(); } static function getGroup() { // 靜態方法 return "default"; }}class User extends DomainObject {}class Document extends DomainObject { static function getGroup() { // 改變了內容 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)
7. final欄位
使類無法被繼承,用的不多
<?phpfinal class Checkout { // 終止類的繼承 // ...}class IllegalCheckout extends Checkout { // ...}$checkout = new Checkout();?>
輸出:
Fatal error: Class IllegalCheckout may not inherit from final class (Checkout)
final方法不能夠被重寫
<?phpclass 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()
8. 解構函式
<?phpclass 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() { // 解構函式 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( '' ); // 最後執行解構函式,使用完之後執行?>
輸出:
do nothing
9. clone方法
複製的時候執行
<?phpclass 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() { // 複製時候執行 $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)
再看一個例子
<?phpclass 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 ) ); // 以類對象作為參數$person->setId( 343 );$person2 = clone $person;// give $person some money$person->account->balance += 10;// $person2 sees the credit tooprint $person2->account->balance; // person的屬性account也是一個類,他的屬性balance的值是210// output:// 210?>
點評:學習還是能夠開拓大腦的,今天終於明白為什麼有多個箭頭的概念了$person->account->balance。這裡的account屬性是一個對象。
10. toString
<?phpclass 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; // 列印時候集中處理// Bob (age 44)?>
點評:必須是print或echo時才有效,print_r就輸出對象。