@param 在类中意味着什么?

发布于 2024-12-28 08:20:19 字数 259 浏览 4 评论 0原文

创建类时@param是什么意思?据我了解,它用于告诉脚本变量是什么类型的数据类型以及函数返回什么类型的值,对吗? 例如:

/**
 * @param string $some
 * @param array $some2
 * @return void
 */

没有其他方法可以做到这一点,我正在考虑类似的事情:void function() { ... } 或类似的东西。对于变量也许 (int)$test;

What does the @param mean when creating a class? As far as I understand it is used to tell the script what kind of datatype the variables are and what kind of value a function returns, is that right?
For example:

/**
 * @param string $some
 * @param array $some2
 * @return void
 */

Isn´t there another way to do that, I am thinking of things like: void function() { ... } or something like that. And for variables maybe (int)$test;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

云胡 2025-01-04 08:20:19

@param 在 PHP 中没有特殊含义,它通常在注释中用于编写文档。您提供的示例说明了这一点。

如果您使用文档工具,它将在代码中搜索 @param@summary 和其他类似值(取决于工具的复杂程度),以自动生成良好的结果代码的格式化文档。

@param doesn't have special meaning in PHP, it's typically used within a comment to write up documentation. The example you've provided shows just that.

If you use a documentation tool, it will scour the code for @param, @summary, and other similar values (depending on the sophistication of the tool) to automatically generate well formatted documentation for the code.

放低过去 2025-01-04 08:20:19

正如其他人提到的,您所指的 @param 是注释的一部分,在语法上并不重要。它只是为文档生成器提供提示。您可以通过查看 PHPDoc 项目找到有关支持的指令和语法的更多信息。

说到你问题的第二部分...据我所知,没有办法在 PHP 中指定返回类型。您也不能强制参数为特定的原始类型(字符串、整数等)。

但是,从 PHP 5.1 左右开始,您可以要求参数是数组、对象或特定类型的对象。

function importArray(array $myArray)
{
}

如果您尝试使用数组以外的任何内容调用此方法,PHP 将抛出错误。

class MyClass
{
}

function doStuff(MyClass $o)
{
}

如果您尝试使用除 MyClass 之外的任何类型的对象调用 doStuff,PHP 将再次抛出错误。

As others have mentioned the @param you are referring to is part of a comment and not syntactically significant. It is simply there to provide hints to a documentation generator. You can find more information on the supported directives and syntax by looking at the PHPDoc project.

Speaking to the second part of your question... As far as I know, there is not a way to specify the return type in PHP. You also can't force the parameters to be of a specific primitive type (string, integer, etc).

You can, however, required that the parameters be either an array, an object, or a specific type of object as of PHP 5.1 or so.

function importArray(array $myArray)
{
}

PHP will throw an error if you try and call this method with anything besides an array.

class MyClass
{
}

function doStuff(MyClass $o)
{
}

If you attempt to call doStuff with an object of any type except MyClass, PHP will again throw an error.

偏爱自由 2025-01-04 08:20:19

我知道这已经过时了,但仅供参考,问题第二部分的答案现在是,PHP7

// this function accepts and returns an int
 function age(int $age): int{

  return 18;
}

I know this is old but just for reference, the answer to the second part of the question is now, PHP7.

// this function accepts and returns an int
 function age(int $age): int{

  return 18;
}
月隐月明月朦胧 2025-01-04 08:20:19

PHP 完全不理会注释。它用于描述该方法所采用的参数,只是为了使代码更具可读性和理解性。

此外,良好的代码实践致力于为文档生成器使用某些名称(例如@param)。

当将鼠标悬停在相关方法上时,某些 IDE 会在工具提示中包含 @param 和其他信息。

PHP is entirely oblivious to comments. It is used to describe the parameters the method takes only for making the code more readable and understandable.

Additionally, good code practice dedicates to use certain names (such as @param), for documentation generators.

Some IDEs will include the @param and other information in the tooltip when using a hovering over the relevant method.

得不到的就毁灭 2025-01-04 08:20:19

@param 是描述注释的一部分,告诉您输入参数类型是什么。它与代码语法无关。使用支持颜色的编辑器(如 Notepad++)轻松查看代码和注释。

@param is a part of the description comment that tells you what the input parameter type is. It has nothing to do with code syntax. Use a color supported editor like Notepad++ to easily see whats code and whats comments.

记忆里有你的影子 2025-01-04 08:20:19

PARAM 是英文“参数”的意思。
PARAM 这个词的含义
PARAM的意思是“参数”,翻译成越南语为“参数”、“参数”。
PARAM 代表什么?
PARAM 中缩写的短语是“参数”。

一些其他类型的缩写 PARAM:

  • 并行机:并行机 - 这是印度超级计算机的一种类型。

PARAM is "Parameter" in English.
Meaning of the word PARAM
PARAM means "Parameter", translated into Vietnamese as "Parameter", "Parameter".
What does PARAM stand for?
The phrase abbreviated in PARAM is “Parameter”.

Some other types of abbreviated PARAM:

  • Parallel Machine: Parallel Machine - this is a type of Indian supercomputer.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文