Req #45569 [Opn->Csd]: Add ReflectionConstant class

From: Date: Mon, 18 Feb 2019 08:38:53 +0000
Subject: Req #45569 [Opn->Csd]: Add ReflectionConstant class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219623@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=45569&edit=1

 ID:                 45569
 Updated by:         nikic@php.net
 Reported by:        missingno at ifrance dot com
 Summary:            Add ReflectionConstant class
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
 Package:            Reflection related
 Operating System:   *
 PHP Version:        5.2CVS-2008-07-20 (snap)
-Assigned To:        
+Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2015-08-31 20:48:52] cmb@php.net

Related To: Bug #53779

------------------------------------------------------------------------
[2015-08-31 20:47:15] 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.

------------------------------------------------------------------------
[2014-02-05 17:48:13] 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

------------------------------------------------------------------------
[2012-03-20 12:01:40] kostia dot lukash at gmail dot com

Any news?

------------------------------------------------------------------------
[2008-07-20 11:40:26] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=45569&edit=1


Thread (5 messages)

« previous php.bugs (#219623) next »