首頁  >  文章  >  後端開發  >  關於PHP物件導向中一些方法、函數應用的程式碼

關於PHP物件導向中一些方法、函數應用的程式碼

不言
不言原創
2018-05-04 17:01:251305瀏覽

這篇文章主要介紹了關於PHP物件導向中的一些知識點,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

1. __construct:  

     內建建構函數,在物件被建立時自動呼叫。請參閱以下程式碼:

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。

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。

 運行結果如下:

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

4. static:

    這裡介紹的static關鍵字主要用於PHP 5.3以上版本新增的延遲靜態綁定功能。請看一下程式碼和關鍵性註解。

printSelf();
SubB::getInstance()->printSelf();

運行結果如下:

Stephens-Air:Desktop$ php Test.php 
This is SubA::printSelf.
This is SubB::printSelf.

     static關鍵字不只可以用於實例化。和self和parent一樣,static也可以作為靜態方法呼叫的標識符,甚至是從非靜態上下文中呼叫。在這個場景下,self仍然表示的是目前方法所在的類別。請參閱下列程式碼: 

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剛好相反,它只是在物件被垃圾收集器收集之前自動調用,我們可以利用該方法做一些必要的清理工作。

運行結果如下:

Stephens-Air:Desktop$ php Test.php 
TestClass destructor is called.
Application will exit.

#6.__clone:

     在PHP 5之后的版本中,对象之间的赋值为引用赋值,即赋值后的两个对象将指向同一地址空间,如果想基于对象赋值,可以使用PHP提供的clone方法。该方法将当前对象浅拷贝之后的副本返回,如果想在clone的过程中完成一些特殊的操作,如深拷贝,则需要在当前类的声明中实现__clone方法,该方法在执行clone的过程中会被隐式调用。另外需要格外注意的是,__clone方法是作用再被拷贝的对象上,即赋值后的对象上执行。

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可以在类中定义常量属性。和全局常量一样,一旦定义就不能改变。常量属性不需要像普通属性那样以$开头,按照惯例,只能用大写字母来命名常量。另外和静态属性一样,只能通过类而不能通过类的实例访问常量属性,引用常量时同样也不需要以$符号作为前导符。另外常量只能被赋值为基础类型,如整型,而不能指向任何对象类型。

运行结果如下:

0Stephens-Air:Desktop$ php Test.php 
TestClass::AVAILABLE = 0

相关推荐:

PHP面向对象 静态延迟绑定static::

浅谈PHP面向对象编程

以上是關於PHP物件導向中一些方法、函數應用的程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn