php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45569 Add ReflectionConstant class
Submitted: 2008-07-20 11:40 UTC Modified: 2019-02-18 08:38 UTC
Votes:6
Avg. Score:4.3 ± 0.7
Reproduced:6 of 6 (100.0%)
Same Version:2 (33.3%)
Same OS:4 (66.7%)
From: missingno at ifrance dot com Assigned: nikic (profile)
Status: Closed Package: Reflection related
PHP Version: 5.2CVS-2008-07-20 (snap) OS: *
Private report: No CVE-ID: None
 [2008-07-20 11:40 UTC] missingno at ifrance dot com
Description:
------------
I'm only submitting a suggestion made by Niels Jaeckel on the Reflection API which I think may be valuable.
See http://docs.php.net/reflection for the original post.


Niels suggested that a new object be created called ReflectionConstant.
ReflectionClass::getConstants() [and maybe also ReflectionExtension::getConstants()] should be changed to return an array of such objects.

This way, it would be possible to read DocComments for class constants.


Also, there is currently no way to retrieve the type hint of a given parameter (in ReflectionParameter). Maybe I should fill another bug report for this one ?

Reproduce code:
---------------
<?php

class MyCharacter { /* some stuff here */ }
class MyString {
  /** ASCII string */
  const TYPE_ASCII = 1;

  /** Unicode string */
  const TYPE_UNICODE = 2;

  public function __construct(MyCharacter $char, $type) {
    /* do something useful here */
  }
}

$class = new ReflectionClass('MyString');
foreach ($class->getConstants as $constant) {
  echo $constant->getName()." = ".$constant->getValue()." (".$contant->getDocComment().")\r\n";
}

echo "\r\n";

$method = new ReflectionMethod('MyString', '__construct');
foreach ($method->getParameters as $parameter) {
  echo "Type hint for ".$param->getName()." is ".$param->getTypeHint().".\r\n";
}

?>

Expected result:
----------------
I would expect something like the following to be echoed:

TYPE_ASCII = 1 (ASCII string)
TYPE_UNICODE = 2 (Unicode string)

Type hint for char is MyCharacter.
Type hint for type is .
// getTypeHint() could return NULL or FALSE if no type hint was given.

Actual result:
--------------
This is currently no way of retrieving the DocComment of a class constant. There is also no way of retrieving information about type hinting in methods' parameters.for

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-23 21:31 UTC] jani@php.net
-Package: Feature/Change Request +Package: Reflection related -Operating System: Any +Operating System: *
 [2012-03-20 12:01 UTC] kostia dot lukash at gmail dot com
Any news?
 [2014-02-05 17:48 UTC] st dot mueller at dzh-online dot de
Interesting, this issue exists for 3,5 years now and nobody seems to care. This is sad. Can at least anyone comment on this, whether this would be an acceptable addition or not? I see there might be problem concerning method namings and BC but what about introducing two new mehtods like getConstantReflector($constant) and getConstantReflectors()?
I really need this feature and use a very expensive workaround for now. I would even try to add a patch for this.

Cheers,

Steve
 [2015-08-31 20:47 UTC] cmb@php.net
> There is also no way of retrieving information about type
> hinting in methods' parameters

That is possible as of PHP 7 with ReflectionParameter::getType(),
see <https://3v4l.org/IU15G>. Formerly only
ReflectionParameter::getClass() to retrieve class type hints was
available.

> This is currently no way of retrieving the DocComment of a class
> constant.

Unfortunately. A PR would be welcome, but the feature might
require an RFC.
 [2017-03-17 12:33 UTC] cmb@php.net
-Summary: Suggestions for reflection API +Summary: Add ReflectionConstant class
 [2019-02-18 08:38 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-02-18 08:38 UTC] nikic@php.net
We have ReflectionClassConstant with a getDocComment() method since PHP 7.1, as part of the introduction of visibility modifiers for constants, so this feature request seems to be fully implemented now.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat May 25 19:01:26 2019 UTC