PHP面向對象中的重要知識點(一)

1. __construct: php

     內置構造函數,在對象被建立時自動調用。見以下代碼:函數

<?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: this

     用於在子類中直接調用父類中的方法,功能等同於Java中的super。 spa

<?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";

     運行結果以下:code

Stephens-Air:Desktop$ php Test.php 
__construct is called...
$arg1 = arg1 $arg2 = arg2 $arg3 = arg3

3. self:對象

     在類內調用該類靜態成員和靜態方法的前綴修飾,對於非靜態成員變量和函數則使用this。 blog

<?php
class StaticExample {
    static public $arg1 = "Hello, This is static field.\n";
    static public function sayHello() {
        print self::$arg1;
    }
}

print StaticExample::$arg1;
StaticExample::sayHello();

     運行結果以下:get

Stephens-Air:Desktop$ php Test.php 
Hello, This is static field.
Hello, This is static field.

4. static:it

     這裏介紹的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和其餘面嚮對象語言相比比較特殊的地方,或者是對我本人而言確實須要簿記下來以備後查的知識點。雖然談不上什麼深度,但仍是但願能與你們分享。

相關文章
相關標籤/搜索