PHP物件導向中的重要知識點(一)

OrangeAdmin發表於2013-12-31

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和其他面嚮物件語言相比比較特殊的地方,或者是對我本人而言確實需要簿記下來以備後查的知識點。雖然談不上什麼深度,但還是希望能與大家分享。

相關文章