pear channel-discover bartlett.laurent-laville.org
PHP_Reflect is a PHP Library that adds the ability to reverse-engineer classes, interfaces, functions, constants, namespaces and more.
This manual documents the final stable version 1.0.2
Uses containers that may be changed to store scanning results
Select properties you want to retrieve (file, start and end lines, docblock, namespace …)
Ability to extend the parsing level: connect user callbacks to handle more tokens
![]() |
The current version of PHP_Reflect requires PHP 5.2.0 or newer to run. If you don’t already have an up-to-date version of PHP installed it can be downloaded from the official PHP website http://www.php.net/. |
PHP_Reflect should be installed using the PEAR Installer. This installer is the backbone of PEAR, which provides a distribution system for PHP packages, and is shipped with every release of PHP since version 4.3.0.
Registering the channel:
pear channel-discover bartlett.laurent-laville.org
Installing the latest version available:
pear install bartlett/PHP_Reflect
Installing a specific version:
pear install bartlett/PHP_Reflect-1.0.0
Do the following:
Download a release archive from http://bartlett.laurent-laville.org/
Extract it to a directory that is listed in the include_path of your php.ini configuration file
You can change default containers (interfaces, classes, functions, includes), and then have different getting methods (rather than standard getInterfaces, … ).
key code | container default name |
---|---|
namespace |
namespaces |
interface |
interfaces |
class |
classes |
function |
functions |
require_once |
includes |
require |
includes |
include_once |
includes |
include |
includes |
variable |
globals |
You can choose what information to retrieve depending of element (key code).
key code | default property |
---|---|
namespace |
file, startEndLines, docblock |
interface |
file, startEndLines, docblock, namespace, keywords, parent, methods |
class |
file, startEndLines, docblock, namespace, keywords, parent, methods, interfaces, package |
function |
file, startEndLines, docblock, namespace, keywords, signature, ccn |
require_once |
file, startEndLines, docblock, namespace |
require |
file, startEndLines, docblock, namespace |
include_once |
file, startEndLines, docblock, namespace |
include |
file, startEndLines, docblock, namespace |
variable |
file, startEndLines, docblock, namespace |
Here we got only keywords and signature of each class methods. Class docblock is NULL (first print_r result) because we have decided NOT to get this property as file, startLine + endLine (startEndLines).
Extends PHP_Reflect core, to match user needs, is possible with usage of the connect() function.
Take a real case. PHP_CompatInfo v2 need to know what are the internal functions used in a source code before to give the minimal php version requires.
To do so, we need to connect a new parser element to token T_STRING. I won’t describe the code of this T_STRING parser here: it’s not the purpose. But we will see how to do.
we add a new container to store internal PHP functions that will be scanned by our new parser element
we connect a user callback that have logic to parse the new token T_STRING
we scan the source file
we get the results (OOP or array access interface)
Final result gave list of 42 PHP functions used in PEAR.php script of PEAR 1.9.2
![]() |
PHP_CompatInfo v2 is an excellent exemple of what is possible to do by connecting other tokens. |
This work is licensed under the BSD License.
The full legal text of the license is given below.
Copyright (c) 2011, Laurent Laville <pear@laurent-laville.org> Credits to Sebastian Bergmann on base concept from phpunit/PHP_Token_Stream All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the authors nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.