像許多其他物件導向的程式語言一樣,PHP 也有一種方法來指示程式內函數的可訪問性。 public、protected 和 private 是使用的關鍵字,其中 public 表示該函數在某個 PHP 程式中是全域可存取的。將函數宣告為 public 有許多優點,其中一個優點就是可以在程式中的任何位置呼叫和使用該函數,而不受任何限制。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
公共功能不受任何限制。公共函數在類別外部、PHP 和其他一些程式語言的程式碼中的類別內部工作。公用函數使其類別中的全部內容僅在其他類別被存取時才可供其他類別使用。儘管它是一個公共函數,但如果不訪問它什麼都不做。 PHP 公用函數如果不在其他類別/類別內存取它,就無法運作/實作任何東西。
以下是一些實現公共功能的範例:
這是公用函數/修飾符的範例。透過下面詳細說明的範例來了解它是如何運作的:
代碼:
<?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(); ?>
輸出:
這是從課堂內部和課堂外部存取公共內容的範例。檢查下面列出的語法。
代碼:
<?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; ?>
輸出:
這是另一個例子。實際上,語法還包含受保護的變量,以便更好地了解程式。
代碼:
<?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 中的公共函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!