搜索
首页后端开发php教程PHP 中的公共函数

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();
?>

输出:

PHP 中的公共函数

示例#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;
?>

输出:

PHP 中的公共函数

示例 #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
简单地说明PHP会话的概念。简单地说明PHP会话的概念。Apr 26, 2025 am 12:09 AM

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInacookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionwwithSession_start()和stordoredAtain $ _session.2)

您如何循环中存储在PHP会话中的所有值?您如何循环中存储在PHP会话中的所有值?Apr 26, 2025 am 12:06 AM

在PHP中,遍历会话数据可以通过以下步骤实现:1.使用session_start()启动会话。2.通过foreach循环遍历$_SESSION数组中的所有键值对。3.处理复杂数据结构时,使用is_array()或is_object()函数,并用print_r()输出详细信息。4.优化遍历时,可采用分页处理,避免一次性处理大量数据。这将帮助你在实际项目中更有效地管理和使用PHP会话数据。

说明如何使用会话进行用户身份验证。说明如何使用会话进行用户身份验证。Apr 26, 2025 am 12:04 AM

会话通过服务器端的状态管理机制实现用户认证。1)会话创建并生成唯一ID,2)ID通过cookies传递,3)服务器存储并通过ID访问会话数据,4)实现用户认证和状态管理,提升应用安全性和用户体验。

举一个如何在PHP会话中存储用户名的示例。举一个如何在PHP会话中存储用户名的示例。Apr 26, 2025 am 12:03 AM

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

哪些常见问题会导致PHP会话失败?哪些常见问题会导致PHP会话失败?Apr 25, 2025 am 12:16 AM

PHPSession失效的原因包括配置错误、Cookie问题和Session过期。1.配置错误:检查并设置正确的session.save_path。2.Cookie问题:确保Cookie设置正确。3.Session过期:调整session.gc_maxlifetime值以延长会话时间。

您如何在PHP中调试与会话相关的问题?您如何在PHP中调试与会话相关的问题?Apr 25, 2025 am 12:12 AM

在PHP中调试会话问题的方法包括:1.检查会话是否正确启动;2.验证会话ID的传递;3.检查会话数据的存储和读取;4.查看服务器配置。通过输出会话ID和数据、查看会话文件内容等方法,可以有效诊断和解决会话相关的问题。

如果session_start()被多次调用会发生什么?如果session_start()被多次调用会发生什么?Apr 25, 2025 am 12:06 AM

多次调用session_start()会导致警告信息和可能的数据覆盖。1)PHP会发出警告,提示session已启动。2)可能导致session数据意外覆盖。3)使用session_status()检查session状态,避免重复调用。

您如何在PHP中配置会话寿命?您如何在PHP中配置会话寿命?Apr 25, 2025 am 12:05 AM

在PHP中配置会话生命周期可以通过设置session.gc_maxlifetime和session.cookie_lifetime来实现。1)session.gc_maxlifetime控制服务器端会话数据的存活时间,2)session.cookie_lifetime控制客户端cookie的生命周期,设置为0时cookie在浏览器关闭时过期。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。