關於PHP物件導向中一些方法、函數應用的程式碼
這篇文章主要介紹了關於PHP物件導向中的一些知識點,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
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
相关推荐:
以上是關於PHP物件導向中一些方法、函數應用的程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4

Visual Studio Code,也稱為 VS Code,是一個免費的原始碼編輯器 - 或整合開發環境 (IDE) - 可用於所有主要作業系統。 VS Code 擁有大量針對多種程式語言的擴展,可以輕鬆編寫

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

字符串是由字符組成的序列,包括字母、數字和符號。本教程將學習如何使用不同的方法在PHP中計算給定字符串中元音的數量。英語中的元音是a、e、i、o、u,它們可以是大寫或小寫。 什麼是元音? 元音是代表特定語音的字母字符。英語中共有五個元音,包括大寫和小寫: a, e, i, o, u 示例 1 輸入:字符串 = "Tutorialspoint" 輸出:6 解釋 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。總共有 6 個元

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。
