©
This document uses PHP Chinese website manual Release
(PHP 5 >= 5.1.0)
ReflectionClass::getDocComment — 获取文档注释
从一个类中获取文档注释。
本函数还未编写文档,仅有参数列表。
此函数没有参数。
如果存在则返回文档注释,否则返回 FALSE
。
Example #1 ReflectionClass::getDocComment() 例子
<?php
class TestClass { }
$rc = new ReflectionClass ( 'TestClass' );
var_dump ( $rc -> getDocComment ())
?>
以上例程会输出:
string(55) ""
[#1] gabe at fijiwebdesign dot com [2014-08-23 23:46:13]
You can also get the docblock definitions for the defined methods of a class as such:
<?php
class Example
{
public function fn()
{
// void
}
}
$reflector = new ReflectionClass('Example');
// to get the Class DocBlock
echo $reflector->getDocComment()
// to get the Method DocBlock
$reflector->getMethod('fn')->getDocComment();
?>
[#2] sun [2014-06-28 14:11:03]
Note that \ReflectionClass::getDocComment() ignores all other PHP code and all white-space between the last encountered T_DOC_COMMENT and the class/element definition.
The only exceptions appear to be T_NAMESPACE declarations and T_FUNCTION definitions.
<?php
namespace Foo;
// ^^ contains excessive leading + trailing white-space.
use Bar\Baz;
const FOO = 'BAR';
$ns = 'bar';
# function foo() {}
$a = 2 + 1;
#
// ^^ A single-line T_DOC_COMMENT is invisible when commented out.
count(array());
class Foo {
}
$reflector = new \ReflectionClass('Foo\Foo');
var_dump($reflector->getDocComment());
?>
yields, despite all the garbage in between:
string(28) ""
To sum up:
1. If there are multiple doc comments, the last encountered applies.
2. Removing the "After namespace." docblock yields FALSE.
(The namespace delimits the scope.)
3. Uncommenting the function definition yields FALSE.
(The doc comment applies to the function instead.)
4. Despite being an own language construct, the "const" constant declaration does not delimit the scope.
5. Any leading and trailing white-space before and after the T_DOC_COMMENT ("") is ignored, but the entire string content within (including all white-space) is consumed literally/verbatim.
[PHP 5.4.29]
[#3] uramihsayibok, gmail, com [2010-09-15 21:50:56]
According to what I can find in the PHP (5.3.2) source code, getDocComment will return the doc comment as the parser found it.
The doc comment (T_DOC_COMMENT) must begin with a . A normal multi-line comment (T_COMMENT) does not count as a doc comment.
The doc comment itself includes those five characters, so
<?php substr($doccomment, 3, -2) ?>
will get you what's inside. A call to trim() after is recommended.
[#4] joe dot scylla at gmail dot com [2009-10-14 08:23:04]
If you're using a bytecode cache like eAccelerator this method will return FALSE even if there is a properly formatted Docblock. It looks like the information required by this method gets stripped out by the bytecode cache.