php @param 在课堂上是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8979046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does @param mean in a class?
提问by Chris
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 是什么意思?据我了解,它用于告诉脚本变量是什么类型的数据类型以及函数返回的值是什么类型,对吗?例如:
/**
* @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;
有没有其他方法可以做到这一点,我在想这样的事情:void function() { ... }
或类似的事情。对于变量可能 (int)$test;
回答by zzzzBov
@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.
@param
在 PHP 中没有特殊含义,通常在注释中用于编写文档。您提供的示例正好说明了这一点。
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.
如果您使用的文档工具,它会冲刷的代码@param
,@summary
以及其他类似的值(取决于工具的复杂程度)来自动生成的代码以及格式的文档。
回答by NuclearDog
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 PHPDocproject.
正如其他人提到的,您所指的 @param 是评论的一部分,在语法上并不重要。它只是为文档生成器提供提示。您可以通过查看PHPDoc项目找到有关支持的指令和语法的更多信息。
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).
谈到你问题的第二部分......据我所知,没有办法在 PHP 中指定返回类型。您也不能强制参数为特定的原始类型(字符串、整数等)。
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.
但是,从 PHP 5.1 开始,您可以要求参数是数组、对象或特定类型的对象。
function importArray(array $myArray)
{
}
PHP will throw an error if you try and call this method with anything besides an array.
如果您尝试使用数组以外的任何内容调用此方法,PHP 将抛出错误。
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.
如果您尝试使用除 之外的任何类型的对象调用 doStuff MyClass
,PHP 将再次抛出错误。
回答by fs_tigre
I know this is old but just for reference, the answer to the second part of the question is now, PHP7
.
我知道这是旧的,但仅供参考,问题第二部分的答案现在是,PHP7
。
// this function accepts and returns an int
function age(int $age): int{
return 18;
}
回答by kba
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.
PHP 完全无视注释。它用于描述方法所采用的参数,只是为了使代码更具可读性和可理解性。
Additionally, good code practice dedicates to use certain names (such as @param
), for documentation generators.
此外,良好的代码实践致力于@param
为文档生成器使用某些名称(例如)。
Some IDEs will include the @param
and other information in the tooltip when using a hovering over the relevant method.
将@param
鼠标悬停在相关方法上时,某些 IDE 将在工具提示中包含和其他信息。
回答by tim
@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.
@param 是描述注释的一部分,它告诉您输入参数类型是什么。它与代码语法无关。使用支持颜色的编辑器(如 Notepad++)轻松查看代码和注释。