像许多其他面向对象的编程语言一样,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中文网其他相关文章!