Reflecting Constants.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi.

I've been using reflection for a while when using SOAP to extract
information about the SOAP type for automatic WSDL generation from the
DocBlock. Subclassing ReflectionClass/ReflectionProperty.

All works lovely. WDSL documents are generated with the correct WSDL
types for things like DATETIME, etc.




I now have a separate use for Reflection. I have a list of class
constants. Primarily they are to assist the developers in reducing the
typos that they make.

e.g.

$s_Module = 'something_that_can_be_misspelled_is_long_winded_and_really_should_be_refactored_but_not_ready_yet';
vs.
$s_Module = Modules::SOMETHING_THAT_BORKS_A_LOT_EARLIER_IF_MISSPELLED_AND_IS_NOT_GOING_TO_BE_THIS_LONG;

Nothing magic here, simply literals => constants.


The names are, unfortunately, not able to be changed in the short
term, and so, this is something we are going to live with for the time
being. But mapping literals to constants is a first step to sanitizing
the code.



Now for my use case. I want to attach some additional information to
the constant - I think these are called annotations, but I see it as
just additional info I can process external to the running code.

/**
 * @vanity LONG_NAME,SHORT_NAME,HUMAN_NAME
 */
const SOMETHING_THAT_BORKS_A_LOT_EARLIER_IF_MISSPELLED_AND_IS_NOT_GOING_TO_BE_THIS_LONG
= 'something_that_can_be_misspelled_is_long_winded_and_really_should_be_refactored_but_not_ready_yet';


So far, so good. All doable.

When a new module is added, the developer only needs to add these
lines to provide a whole set of additional information that can be
used downstream without the need to jump between the different systems
that will use this additional info. (In this case, this data is used
to provide various forms of the module names - one of which is human
name - which get stored in a DB and these get translated for the
multi-lingual vanity URLs).


My issue is that there is no ReflectionConstant mechanism.

I'm currently using the TokenReflection classes (essentially parses
the class and wraps all the results up in a Reflection API). The
parsing is only done at the end of a chain of events and only as a
last resort, so I'm not worried about performance in this case. But
the TokenReflection code is significantly more than I want at the
moment. It is yet another library for what would only be 1 method.


It would be so nice to have something native or simpler.


I did start with using the built in Reflection classes, but
ReflectionClass::export('Module') correctly shows all the docblocks
for the class, methods and properties, and it shows the constants, but
no docblocks for the constants.


Running the following code will show the issue ...

<?php
/**
 * This is a simple class.
 */
class Simple{
    /**
     * This is a class constant.
     */
    const A_CONSTANT = 'not flexible';

    /**
     * This is a method.
     */
    public function display(){
        echo self::A_CONSTANT;
    }
}

$s_Reflected = ReflectionClass::export('Simple');
?>

This outputs ...

/**
 * This is a simple class.
 */
Class [ <user> class Simple ] {
  @@ - 6-18

  - Constants [1] {
    Constant [ string A_CONSTANT ] { not flexible }
  }

  - Static properties [0] {
  }

  - Static methods [0] {
  }

  - Properties [0] {
  }

  - Methods [1] {
    /**
     * This is a method.
     */
    Method [ <user> public method display ] {
      @@ - 15 - 17
    }
  }
}


It would seem that without tokenising the class file, I'm sort of
stuck with the solution I've got at the moment.

Unless anyone has any ideas!

Regards,

Richard.


-- 
Richard Quadling
Twitter : @RQuadling
EE : http://e-e.com/M_248814.html
Zend : http://bit.ly/9O8vFY

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux