PHP 5.6.0 released

ReflectionClass::getDocComment

(PHP 5 >= 5.1.0)

ReflectionClass::getDocCommentObtener los comentarios de documentación

Descripción

public string ReflectionClass::getDocComment ( void )

Obtiene los comentarios de documentación de una clase.

Advertencia

Esta función no está documentada actualmente, solamente se encuentra disponible la lista de parámetros.

Parámetros

Esta función no tiene parámetros.

Valores devueltos

Los comentarios de documentación, o FALSE si no existiera.

Ejemplos

Ejemplo #1 Ejemplo de ReflectionClass::getDocComment()

<?php
/** 
* Clase de prueba
*
* @param  foo bar
* @return baz
*/
class ClaseDePrueba { }

$rc = new ReflectionClass('ClaseDePrueba');
var_dump($rc->getDocComment())
?>

El resultado del ejemplo sería:

string(55) "/** 
* Clase de prueba
*
* @param  foo bar
* @return baz
*/"

Ver también

add a note add a note

User Contributed Notes 4 notes

up
7
joe dot scylla at gmail dot com
4 years ago
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.
up
5
uramihsayibok, gmail, com
3 years ago
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 /** - that's two asterisks, not one. The comment continues until the first */. 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.
up
1
sun
2 months ago
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
/**
* Before namespace.
*/
namespace Foo;

/**
* After namespace.
   */ 

// ^^ contains excessive leading + trailing white-space.
use Bar\Baz;
const
FOO = 'BAR';
$ns = 'bar';
# function foo() {}
$a = 2 + 1;
#/** what? */
// ^^ 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) "/**
* After namespace.
   */"

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]
up
0
gabe at fijiwebdesign dot com
9 days ago
You can also get the docblock definitions for the defined methods of a class as such:

<?php
/**
* This is an Example class
*/
class Example
{
   
/**
     * This is an example function
     */
   
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();

?>
To Top