PHP 中的公共函数
Aug 29, 2024 pm 01:08 PM
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中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发
