首页  >  文章  >  后端开发  >  PHP 注释

PHP 注释

PHPz
PHPz原创
2024-08-29 13:02:09580浏览

PHP 注释基本上是元数据,可以包含在源代码中,也可以包含在类、函数、属性和方法之间。无论在何处声明它们,它们都以前缀 @ 开头,并且它们指示特定的内容。他们提供的这些信息对编码人员非常有用,有助于文档目的,IDE 也可以使用它来显示某些弹出提示之类的东西。除了验证之外,相同的注释还可以用于其他目的,例如确定需要在表单中给出哪种输入以及用于自动化目的。有各种类型的注释,例如 @var 和 @int 类型,正如其名称本身所暗示的那样,它们可用于特定用途。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

PHP注解是通过@前缀来使用的,其语法如下:

class Example
{
// @var integer
public $new;
}

这里的注释是@var,每当在任何代码片段之前遇到它时(例如这里的public $new),它表明$new将具有注释所告诉的整数类型的值。

class Example
{
// @var integer
// @range(0, 51)
// @label('Count of shops')
public $shop;
}

注释还可以用于指定显示函数整数值的最大值和最小值的范围,并且标签给出了该函数的用途。

PHP 注解的类型

以下是类型:

1.内置注释

注解中有2个内置函数,如下:

a。 Compiled: 该注解指示该方法/函数是否应该进行 JIT 编译。它也是函数范围类型的注释。

b。 SuppressWarnings:这是另一个内置注释,这意味着必须抑制在执行其下面的后续代码时引发的任何警告。

2.元注释

这些是可用于申请其他注释的注释类型。它们用于配置注释。

a。 @注释

有一种注释类将包含@annotation。

代码:

[@Annotation]
class MyAnnoExample {
// piece of code
}

b。 @目标

顾名思义,该注解指示了该注解适用的类元素类型或方法。

据此我们可以描述一个或多个目标:

  • 属性注释位于属性类声明之前。
  • 在声明类之前允许的类。
  • 函数在函数声明之前声明。
  • 方法注释允许继续进行方法声明。
  • 允许使用注解来继续声明注解类。

c. @可重复

这个注解意味着它在使用时可以重复任意次数。

d。 @继承

这也可以用作其他用户定义的注释类作为元注释。这些继承的注释在超类上使用时会自动继承到各自的子类。

3.自定义注释

这些与普通类的声明非常相似。注释类型的每个元素都由每个属性声明定义。

PHP 注解示例

下面给出的是提到的示例:

示例#1

代码:

// namespace declaration here
[@Annotation]
[@Target("class")]
class MyAnnoEx {
[@Required]
public string $prop;
public array $arrayProp = [];
public embedAnno $embed;
}
[@Annotation]
// code for embedded annotation goes here
[@Target(["class", "annotation"])]
class embedAnno {
}
[@Annotation]
// example for target annotation
[@Target("property")]
class propAnno {
}
@Annotation
// code for method annotation goes here
@Target("method")
class methodAnno {
public string $val;
public function __construct(string $val) {
$this->val = $val;
}
}

这只是一个基本示例,显示了上面显示的所有不同类型注释的用法。示例中的嵌入注解、属性注解、方法注解等都是自定义注解。

示例#2

代码:

<!DOCTYPE html>
<html>
<body>
<?php
/**
* @Replace("exmaple", "for", "annotation")
*/
class MyNamedComponent
{
}
echo str_replace("First", "Second", "First Example");
?>
</body>
</html>

输出:

PHP 注释

在这个例子中,我们将注释命名为replace,因为下面的代码代表了字符串替换函数的用法,即str_replace,PHP的内置函数。使用此函数,函数中传递的第一个参数将被第二个参数替换。

示例#3

代码:

<!DOCTYPE html>
<html>
<head>
<title>Simple Form Processing</title>
</head>
<body>
<h1>Form Processing using PHP</h1>
<fieldset>
<form id="formex1" method="post" action="formexample.php">
<!--Declaring First name for the form
@Annotation text first_name-->
First_Name:
<input type="text" name="First_Name"/>
<!--@var style color-->
<span style="color:blue;">*</span>
<br>
<br>
<!--Declaring Last_Name for the form
@Annotation text last_name-->
Last_Name:
<input type="text" name="last_name"/>
<span style="color:blue;">*</span>
<br>
<br>
<!--Declaring Location for the form
@Annotation text location-->
Stay location:
<input type="text" name="location"/>
<span style="color:blue;">*</span>
<br>
<br>
<!--Declaring EMAILID for the form
@Annotation text email-->
EmailID:
<input type="email" name="emailID"/>
<span style="color:blue;">*</span>
<br>
<br>
<!--Declaring Password for the form
@Annotation password-->
Password:
<input type="password" name="password"/>
<span style="color:blue;">*</span>
<br>
<br>
<!--Declaring Password for the form
@Radio button password-->
Gender:
<!-- Gender to be selected as either male or female -->
<input type="radio"
value="Male"
name="gender"> Male
<input type="radio"
value="Female"
name="gender">Female
<br>
<br>
<input type="confirm" value="confirm" name="confirm" />
</form>
</fieldset>
<?php
if(example($_POST['confirm']))
{
if(!example($error))
{
echo"<h1>DETAILS RECEIVED</h1><br>";
echo "<table border='2'>";
echo "<thead>";
echo "<th>Argument</th>";
echo "<th>Value</th>";
echo "</thead>";
echo "<tr>";
echo "<td>First Name</td>";
echo "<td>".$First_Name."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Last_Name</td>";
echo "<td>".$last_name."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Stay location</td>";
echo "<td>".$location."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Email Stay location</td>";
echo "<td>" .$emailID."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Password</td>";
echo "<td>".$password."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Gender</td>";
echo "<td>".$gender."</td>";
echo "</tr>";
echo "</table>";
}
}
?>
</body>
</html>

输出:

PHP 注释

在此示例中,我们结合 PHP 中的表单验证来展示注释。使用注释,我们标记了表单输入参数所需的所有参数,例如名字和姓氏、电子邮件、位置和密码。

结论

通过上面的示例,我们注意到注释是如何成为使用和表达有关我们的方法、类或属性的元数据的强大工具。我们还了解了如何组合不同类型的注释来通过编写有关工作人员的一些元数据来声明将执行某些任务的工作人员。这使得它们很容易找到,并提供有关它们是否可以使用的实际信息。

以上是PHP 注释的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP GET Method下一篇:PHP Encryption