Source for file reflection_php5.php
Documentation is available at reflection_php5.php
* base include file for SimpleTest
* @version $Id: reflection_php5.php 1683 2008-03-05 21:57:08Z lastcraft $
* Version specific reflection API.
* Stashes the class/interface.
* @param string $interface Class or interface
* Checks that a class has been declared. Versions
* before PHP5.0.2 need a check that it's not really
* @return boolean True if defined.
$reflection =
new ReflectionClass($this->_interface);
return ! $reflection->isInterface();
* Needed to kill the autoload feature in PHP5
* for classes created dynamically.
* @return boolean True if defined.
* Checks that a class or interface has been
* @return boolean True if defined.
return $this->_classOrInterfaceExistsWithAutoload($this->_interface, true);
* Needed to kill the autoload feature in PHP5
* for classes created dynamically.
* @return boolean True if defined.
return $this->_classOrInterfaceExistsWithAutoload($this->_interface, false);
* Needed to select the autoload feature in PHP5
* for classes created dynamically.
* @param string $interface Class or interface name.
* @param boolean $autoload True totriggerautoload.
* @return boolean True if interface defined.
function _classOrInterfaceExistsWithAutoload($interface, $autoload) {
* Gets the list of methods on a class or
* @returns array List of method names.
* Gets the list of interfaces from a class. If the
* class name is actually an interface then just that
* @returns array List of interfaces.
$reflection =
new ReflectionClass($this->_interface);
if ($reflection->isInterface()) {
return $this->_onlyParents($reflection->getInterfaces());
* Gets the list of methods for the implemented
* @returns array List of enforced method signatures.
* Checks to see if the method signature has to be tightly
* @param string $method Method name.
* @returns boolean True if enforced.
function _isInterfaceMethod($method) {
* Finds the parent class name.
* @returns string Parent class name.
$reflection =
new ReflectionClass($this->_interface);
$parent =
$reflection->getParentClass();
return $parent->getName();
* Trivially determines if the class is abstract.
* @returns boolean True if abstract.
$reflection =
new ReflectionClass($this->_interface);
return $reflection->isAbstract();
* Trivially determines if the class is an interface.
* @returns boolean True if interface.
$reflection =
new ReflectionClass($this->_interface);
return $reflection->isInterface();
* Scans for final methods, as they screw up inherited
* mocks by not allowing you to override them.
* @returns boolean True if the class has a final method.
$reflection =
new ReflectionClass($this->_interface);
foreach ($reflection->getMethods() as $method) {
if ($method->isFinal()) {
* Whittles a list of interfaces down to only the
* necessary top level parents.
* @param array $interfaces Reflection API interfaces
* @returns array List of parent interface names.
function _onlyParents($interfaces) {
foreach ($interfaces as $interface) {
foreach($interfaces as $possible_parent) {
if ($interface->getName() ==
$possible_parent->getName()) {
if ($interface->isSubClassOf($possible_parent)) {
$blacklist[$possible_parent->getName()] =
true;
if (!isset
($blacklist[$interface->getName()])) {
$parents[] =
$interface->getName();
* Checks whether a method is abstract or not.
* @param string $name Method name.
* @return bool true if method is abstract, else false
function _isAbstractMethod($name) {
$interface =
new ReflectionClass($this->_interface);
if (! $interface->hasMethod($name)) {
return $interface->getMethod($name)->isAbstract();
* Checks whether a method is the constructor.
* @param string $name Method name.
* @return bool true if method is the constructor
function _isConstructor($name) {
return ($name ==
'__construct') ||
($name ==
$this->_interface);
* Checks whether a method is abstract in all parents or not.
* @param string $name Method name.
* @return bool true if method is abstract in parent, else false
function _isAbstractMethodInParents($name) {
$interface =
new ReflectionClass($this->_interface);
$parent =
$interface->getParentClass();
if (! $parent->hasMethod($name)) {
if ($parent->getMethod($name)->isAbstract()) {
$parent =
$parent->getParentClass();
* Checks whether a method is static or not.
* @param string $name Method name
* @return bool true if method is static, else false
function _isStaticMethod($name) {
$interface =
new ReflectionClass($this->_interface);
if (! $interface->hasMethod($name)) {
return $interface->getMethod($name)->isStatic();
* Writes the source code matching the declaration
* @param string $name Method name.
* @return string Method signature up to last
return 'function __set($key, $value)';
return 'function __call($method, $arguments)';
if (in_array($name, array('__get', '__isset', $name ==
'__unset'))) {
return "function {$name}(\$key)";
if ($name ==
'__toString') {
return "function $name()";
if ($this->_isInterfaceMethod($name) ||
$this->_isAbstractMethod($name) ||
$this->_isAbstractMethodInParents($name) ||
$this->_isStaticMethod($name)) {
return $this->_getFullSignature($name);
return "function $name()";
* For a signature specified in an interface, full
* details must be replicated to be a valid implementation.
* @param string $name Method name.
* @return string Method signature up to last
function _getFullSignature($name) {
$interface =
new ReflectionClass($this->_interface);
$method =
$interface->getMethod($name);
$reference =
$method->returnsReference() ?
'&' :
'';
$static =
$method->isStatic() ?
'static ' :
'';
return "{
$static}function
$reference$name(
" .
implode(', ', $this->_getParameterSignatures($method)) .
* Gets the source code for each parameter.
* @param ReflectionMethod $method Method object from
* @return array List of strings, each
function _getParameterSignatures($method) {
foreach ($method->getParameters() as $parameter) {
$type =
$parameter->getClass();
$signature .=
$type->getName() .
' ';
if ($parameter->isPassedByReference()) {
$signature .=
'$' .
$this->_suppressSpurious($parameter->getName());
if ($this->_isOptional($parameter)) {
$signatures[] =
$signature;
* The SPL library has problems with the
* Reflection library. In particular, you can
* get extra characters in parameter names :(.
* @param string $name Parameter name.
* @return string Cleaner name.
function _suppressSpurious($name) {
* Test of a reflection parameter being optional
* that works with early versions of PHP5.
* @param reflectionParameter $parameter Is this optional.
* @return boolean True if optional.
function _isOptional($parameter) {
return $parameter->isOptional();
Documentation generated on Sun, 04 May 2008 09:21:59 -0500 by phpDocumentor 1.3.0