这里有两个类似的问题,但是没有一个答案似乎有效.
PHPDoc似乎没有将我的函数中的可选参数识别为可选参数,例如:
/**
* Opens the connection and sets encoding
*
* @param string $encoding Encoding.
*/
public function __construct($encoding='UTF-8')
{
$this->connect_mysqli();
$this->set_encoding_mysqli($encoding);
}
Run Code Online (Sandbox Code Playgroud)
它不应该认为$ encoding是可选的,或者我在这里遗漏了什么?我真的试着谷歌并阅读文档,但我找到的只是:
如果你没有在实际代码中指出参数是可选的(通过"$ paramname ='默认值'"),那么你应该在参数的描述中提到参数是可选的.
所以我看到我的代码没有问题,但我在文档中得到的是:"__ construct(string $ encoding)",在参数是可选的任何地方都没有任何符号.
假设我有一个类,有这样的方法:
/*
*
* Loads the user from username.
*
* @param string $username The username
*
* @return UserInterface
*
* @throws userNotFoundException if the user is not found
*/
public function getUser($username)
{
// someFunction return an UserInterface class if found, or null if not.
$user = someFunction('SELECT ....', $username);
if ($user === null) {
throw new userNotFoundException();
}
return $user
}
Run Code Online (Sandbox Code Playgroud)
现在让我们说someFunction可以抛出InvalidArgumentException/ RuntimeException/ PDOException出于XYZ的原因.我该怎么办?什么不是?
添加可能someFunction在php-docs中引发的所有可能异常.
/*
* …Run Code Online (Sandbox Code Playgroud) 我有一个方法从数据库中提取修改过的预序树横向树,并使用回调函数对其进行过滤.例如:
/**
* Recursive function for building the Cas_Template_TreeNode.
*
* @static
* @param array $rows
* @param callback $filter A function to filter the tree by (return a value convertible to false to remove the item from the tree)
* @return array
*/
private static function MakeTreeGivenDbRows($rows, $filter = null)
{
if ($filter === null)
{
$filter = function($unused)
{
return true;
};
}
$result = array();
$childrenCount = 0;
for ($idx = 0; $idx < count($rows); $idx += …Run Code Online (Sandbox Code Playgroud) 我正在玩PHPDoc,并意识到你可以使用markdown为DocBlock添加一些格式.特别是,我注意到您可以使用后退标记来突出显示内联代码.
但是,我似乎无法弄清楚如何向DocBlock添加代码块,因为使用4个空格似乎不起作用.
我已经尝试使用<code>,并<pre>过了,而这些标签也出现在生成的文档,里面它们的代码变得与HTML注释注释掉.
例如,这个DocBlock:
/**
* This is a test DocBlock
*
* <pre>
* <?php
* echo('hi');
* ?>
* </pre>
*
* @return object[] An array of objects.
*/
Run Code Online (Sandbox Code Playgroud)
生成此HTML:
<pre>
<!--?php echo('hi'); ?-->
</pre>
Run Code Online (Sandbox Code Playgroud)
我哪里错了?如何向DocBlock添加代码块?
从我所做的研究来看,我似乎无法找到格式化多行phpdoc @param线的正确方法.建议的方法是什么?
这是一个例子:
/**
* Prints 'Hello World'.
*
* Prints out 'Hello World' directly to the output.
* Can be used to render examples of PHPDoc.
*
* @param string $noun Optional. Sends a greeting to a given noun instead.
* Input is converted to lowercase and capitalized.
* @param bool $surprise Optional. Adds an exclamation mark after the string.
*/
function helloYou( $noun = 'World', $surprise = false ) {
$string = 'Hello ' . ucwords( strtolower( …Run Code Online (Sandbox Code Playgroud) 考虑以下PHP 5类:
class SomeClass
{
//I want to document this property...
private $foo;
function __construct()
{
}
public function SetFoo($value)
{
$this->foo = $value;
}
public function GetFoo()
{
return $this->foo;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在phpDocumentor中记录$ foo属性?我甚至不确定它是否需要记录,但我想知道如果需要......
我知道如何记录SetFoo()和GetFoo(),我只是不确定私有属性(变量?).
谢谢!
我正在使用Eclipse PDT,我想使用Phpdoc注释一个局部变量.
我所看到的是,我可以使用@var或甚至
注释类的变量/属性@property,但这对于局部变量有何可能?
我怎么能这样做?
function foo(){
/** @var Stock $a */
$a->save();
}
Run Code Online (Sandbox Code Playgroud) 我在PHP中使用Reflection API从方法中提取DocComment(PHPDoc)字符串
$r = new ReflectionMethod($object);
$comment = $r->getDocComment();
Run Code Online (Sandbox Code Playgroud)
这将返回一个类似于此的字符串(取决于方法记录的程度)
/**
* Does this great things
*
* @param string $thing
* @return Some_Great_Thing
*/
Run Code Online (Sandbox Code Playgroud)
是否有任何可以将PHP Doc Comment String解析为数据结构的内置方法或函数?
$object = some_magic_function_or_method($comment_string);
echo 'Returns a: ', $object->return;
Run Code Online (Sandbox Code Playgroud)
缺乏这一点,我应该看看PHPDoc源代码的哪一部分.
缺乏和/或除此之外,是否有第三方代码被认为是"更好"的PHPDoc代码?
我意识到解析这些字符串不是火箭科学,甚至不是计算机科学,但我更喜欢一个经过良好测试的库/例程/方法,它是为了处理许多janky,半非正确的PHP Doc代码而构建的.可能存在于野外.
为构造函数和类和仅包含单个类的文件一致地编写注释块的最有用/最标准/最不令人惊讶的方法是什么?
然后文件本身?如果它只包含一个类,是否需要注释块?应该去哪些细节?
我想尽量避免在类,构造函数和文件注释块之间重复.
如果我只想从父级继承所有文档,那么phpDocumentor中@inheritDoc的正确语法是什么?也许不止一种语法是正确的?
@inheritDoc{@inheritDoc}@inheritdoc{@inheritdoc}我认为文档很模糊.PhpStorm似乎支持所有这些,但也许我会在使用一些语法生成文档时遇到麻烦?