PHP 中的公共函數
像許多其他物件導向的程式語言一樣,PHP 也有一種方法來指示程式內函數的可訪問性。 public、protected 和 private 是使用的關鍵字,其中 public 表示該函數在某個 PHP 程式中是全域可存取的。將函數宣告為 public 有許多優點,其中一個優點就是可以在程式中的任何位置呼叫和使用該函數,而不受任何限制。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
公用函數在 PHP 中如何運作?
公共功能不受任何限制。公共函數在類別外部、PHP 和其他一些程式語言的程式碼中的類別內部工作。公用函數使其類別中的全部內容僅在其他類別被存取時才可供其他類別使用。儘管它是一個公共函數,但如果不訪問它什麼都不做。 PHP 公用函數如果不在其他類別/類別內存取它,就無法運作/實作任何東西。
在 PHP 中實作公用函數的範例
以下是一些實現公共功能的範例:
範例#1
這是公用函數/修飾符的範例。透過下面詳細說明的範例來了解它是如何運作的:
代碼:
<?php // BaseClass class publ { public $tagging_line = "Scientists & Engineers are the real Geeks"; function display() { echo $this->tagging_line."\n"; } } // SubClass class subc extends publ { function show(){ echo $this->tagging_line; } } // Object Declaration $obj= new subc; // Scientists & Engineers are the real Geeks! echo $obj->tagging_line."\n"; // Scientists & Engineers are the real Geeks! $obj->display(); // Scientists & Engineers are the real Geeks! $obj->show(); ?>
輸出:
範例#2
這是從課堂內部和課堂外部存取公共內容的範例。檢查下面列出的語法。
代碼:
<?php class Itemone { /** * This is the INSIDE PROGRAMMING CODE because it is actually written INSIDE of the class. */ public $labelone; public $priceone; } /** * This is OUTSIDE PROGRAMMING CODE because it is actually written OUTSIDE of the class. */ $item = new Itemone(); $item->labelone = ' Phantam Drone - Mavic Pro '; $item->priceone = 250.99; echo $item->labelone; /** * Printing your variable value which contains string. $item is the public function variable declaration to store values accessing from Item function */ echo $item->priceone; ?>
輸出:
範例 #3
這是另一個例子。實際上,語法還包含受保護的變量,以便更好地了解程式。
代碼:
<?php class Itemone { /** * Here's the new INSIDE PROGRAMMING CODE and the Rules Which are to follow: * * 1. It will STOP ACCESS to the properties of it via $itemone->labelone and $itemone >priceone, * with the help of the protected keyword. * 2. FORCING the use of the public functions in the code. * 3. The ONLY strings are now allowed OUT & IN of this class/classes for $labelone * with the help of the getLabelone and setLabelone functions. * 4. In OUT & IN of the class only floats are allowed now for $priceone * by using getPriceone and setPriceone functions. */ protected $labelone = 'Unknown ItemONE'; // Rule 1 - protected Variable. protected $priceone = 0.0; // Rule 1 - protected Variable. public function getLabelone() { // Rule 2 - public function declaration. return $this->labelone; // Rule 3 - string OUT for $labelone. } public function getPriceone() { // Rule 2 - public function declaration for Priceone. return $this->priceone; // Rule 4 - The float OUT for $priceone. } public function setLabelone($labelone) // Rule 2 - public function declaration. { /** * Make sure $labelone is a PHP string that can be used in a SORTING * alogorithm, number, array, NOT a boolean or the object that can't be * properly sorted -- AND to make sure that the getLabelone() function * ALWAYS returns PHP string which is genuine. * * Using a RegExp would now improve this function, however, the main * point is the one made above. */ if(is_string($labelone)) { $this->labelone = (string)$labelone; // Rule 3 - string IN for $label. } } public function setPriceone($priceone) // Rule 2 - public function. { /** * Make sure $priceone is a PHP float value so that it can be used in a particular * NUMERICAL CALCULATION. Do not accept string, array, boolean or * some of the other object/objects that can't be included in a simple calculation. * Now This will ensure that the getPriceone() function will ALWAYS returns * genuine, authentic and also full-flavored PHP's number and nothing but. * * Checking the positive values may/might improve this one function, * however, the main point is the one made above. */ if(is_numeric($priceone)) { $this->priceone = (float)$priceone; // Rule 4 - float IN for $price. } } } ?>
優點是
以下是公用函數的一些優點,解釋如下:
- 它可以在整個程式/專案的任何地方存取。我的意思是 PHP 中的類別或公共函數的公共方法可以在類別外部或內部或子類別中呼叫。
- 它可以在班級外部訪問,也可以在班級內部從另一個班級訪問。
- 這個public使得存取功能沒有任何限制。它就像特定物件的公共財產。您可以從程式內的任何位置修改或檢索它。
- 公用函數將顯示/提供程式碼的完整意圖。
- 僅在存取時才在程式範圍內可見。
規則與條例
以下是一些公共職能的規則和規定,解釋如下:
- 公有方法/函數/修飾符/關鍵字可以在類別外部不受任何限制地調用,也可以在類別內部存取。
- 當公用函數的程式碼需要執行其程式碼指令時,應該存取公用函數/修飾符,否則公用函數將不執行任何操作。
- 使用公用函數/類似的方式在類別內存取。
以上是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

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

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

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

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

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

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

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7
