1. __construct:
內置構造函數,在對象被創建時自動調用。見如下代碼:
<?php class ConstructTest { private $arg1; private $arg2; public function __construct($arg1, $arg2) { $this->arg1 = $arg1; $this->arg2 = $arg2; print "__construct is called...\n"; } public function printAttributes() { print '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2."\n"; } } $testObject = new ConstructTest("arg1","arg2"); $testObject->printAttributes();
運行結果如下:
Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2
2. parent:
用於在子類中直接調用父類中的方法,功能等同於Java中的super。
<?php class BaseClass { protected $arg1; protected $arg2; function __construct($arg1, $arg2) { $this->arg1 = $arg1; $this->arg2 = $arg2; print "__construct is called...\n"; } function getAttributes() { return '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2; } } class SubClass extends BaseClass { protected $arg3; function __construct($baseArg1, $baseArg2, $subArg3) { parent::__construct($baseArg1, $baseArg2); $this->arg3 = $subArg3; } function getAttributes() { return parent::getAttributes().' $arg3 = '.$this->arg3; } } $testObject = new SubClass("arg1","arg2","arg3"); print $testObject->getAttributes()."\n";
運行結果如下:
Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2 $arg3 = arg3
3. self:
在類內調用該類靜態成員和靜態方法的前綴修飾,對於非靜態成員變量和函數則使用this。
<?php class StaticExample { static public $arg1 = "Hello, This is static field.\n"; static public function sayHello() { print self::$arg1; } } print StaticExample::$arg1; StaticExample::sayHello();
運行結果如下:
Stephens-Air:Desktop$ php Test.php
Hello, This is static field.
Hello, This is static field.
4. static:
這里介紹的static關鍵字主要用於PHP 5.3以上版本新增的延遲靜態綁定功能。請看一下代碼和關鍵性注釋。
<?php abstract class Base { public static function getInstance() { //這里的new static()實例化的是調用該靜態方法的當前類。 return new static(); } abstract public function printSelf(); } class SubA extends Base { public function printSelf() { print "This is SubA::printSelf.\n"; } } class SubB extends Base { public function printSelf() { print "This is SubB::printSelf.\n"; } } SubA::getInstance()->printSelf(); SubB::getInstance()->printSelf();
運行結果如下:
Stephens-Air:Desktop$ php Test.php
This is SubA::printSelf.
This is SubB::printSelf.
static關鍵字不僅僅可以用於實例化。和self和parent一樣,static還可以作為靜態方法調用的標識符,甚至是從非靜態上下文中調用。在該場景下,self仍然表示的是當前方法所在的類。見如下代碼:
<?php abstract class Base { private $ownedGroup; public function __construct() { //這里的static和上面的例子一樣,表示當前調用該方法的實際類。 //需要另外說明的是,這里的getGroup方法即便不是靜態方法,也會得到相同的結果。然而倘若 //getGroup真的只是普通類方法,那么這里還是建議使用$this。 $this->ownedGroup = static::getGroup(); } public function printGroup() { print "My Group is ".$this->ownedGroup."\n"; } public static function getInstance() { return new static(); } public static function getGroup() { return "default"; } } class SubA extends Base { } class SubB extends Base { public static function getGroup() { return "SubB"; } } SubA::getInstance()->printGroup(); SubB::getInstance()->printGroup();
運行結果如下:
Stephens-Air:Desktop$ php Test.php
My Group is default
My Group is SubB
5. __destruct:
析構方法的作用和構造方法__construct剛好相反,它只是在對象被垃圾收集器收集之前自動調用,我們可以利用該方法做一些必要的清理工作。
<?php class TestClass { function __destruct() { print "TestClass destructor is called.\n"; } } $testObj = new TestClass(); unset($testObj); print "Application will exit.\n";
運行結果如下:
Stephens-Air:Desktop$ php Test.php
TestClass destructor is called.
Application will exit.
6. __clone:
在PHP 5之后的版本中,對象之間的賦值為引用賦值,即賦值后的兩個對象將指向同一地址空間,如果想基於對象賦值,可以使用PHP提供的clone方法。該方法將當前對象淺拷貝之后的副本返回,如果想在clone的過程中完成一些特殊的操作,如深拷貝,則需要在當前類的聲明中實現__clone方法,該方法在執行clone的過程中會被隱式調用。另外需要格外注意的是,__clone方法是作用再被拷貝的對象上,即賦值后的對象上執行。
<?php class InnerClass { public $id = 10; public function printSelf() { print '$id = '.$this->id."\n"; } } class OuterClass { public $innerClass; public function __construct() { $this->innerClass = new InnerClass(); } public function __clone() { $this->innerClass = clone $this->innerClass; print "__clone is called.\n"; } } $outerA = new OuterClass(); print "Before calling to clone.\n"; $outerB = clone $outerA; print "After calling to clone.\n"; $outerA->innerClass->id = 20; print "In outerA: "; $outerA->innerClass->printSelf(); print "In outerB: "; $outerB->innerClass->printSelf();
運行結果如下:
Stephens-Air:Desktop$ php Test.php Before calling to clone. __clone is called. After calling to clone. In outerA: $id = 20 In outerB: $id = 10
7. const:
PHP5可以在類中定義常量屬性。和全局常量一樣,一旦定義就不能改變。常量屬性不需要像普通屬性那樣以$開頭,按照慣例,只能用大寫字母來命名常量。另外和靜態屬性一樣,只能通過類而不能通過類的實例訪問常量屬性,引用常量時同樣也不需要以$符號作為前導符。另外常量只能被賦值為基礎類型,如整型,而不能指向任何對象類型。
<?php class TestClass { const AVAILABLE = 0; } print "TestClass::AVAILABLE = ".TestClass::AVAILABLE."\n";
運行結果如下:
0Stephens-Air:Desktop$ php Test.php TestClass::AVAILABLE = 0
注:該Blog中記錄的知識點,是在我學習PHP的過程中,遇到的一些PHP和其他面向對象語言相比比較特殊的地方,或者是對我本人而言確實需要簿記下來以備后查的知識點。雖然談不上什么深度,但還是希望能與大家分享。