Reflection in PHP

Definition:
In der Programmierung bedeutet Reflexion (engl. reflection) oder Introspektion, dass ein Programm seine eigene Struktur kennt und diese, wenn nötig, modifizieren kann.

Gibt es seit PHP 5.

1.) ReflectionClass — The ReflectionClass class => Class Reflection

  • ReflectionClass::getConstants — Gets constants
  • ReflectionClass::getConstructor — Gets constructor
  • ReflectionClass::getDocComment — Gets doc comments
  • ReflectionClass::getFileName — Gets the filename of the file in which the class has been defined
  • ReflectionClass::getInterfaces — Gets the interfaces
  • ReflectionClass::getMethods — Gets a list of methods
  • ReflectionClass::getParentClass — Gets parent class
  • ReflectionClass::getProperties — Gets properties
  • ReflectionClass::getStartLine — Gets starting line number
  • ReflectionClass::getStaticProperties — Gets static properties
  • ReflectionClass::implementsInterface — Implements interface
  • ReflectionClass::inNamespace — Checks if in namespace
  • ReflectionClass::isAbstract — Checks if class is abstract
  • ReflectionClass::isFinal — Checks if class is final
  • ReflectionClass::isInstance — Checks class for instance
  • ReflectionClass::isInstantiable — Checks if instantiable
  • ReflectionClass::isInterface — Checks if interface
  • ReflectionClass::isSubclassOf — Checks if a subclass
  • ReflectionClass::isUserDefined — Checks if user defined
  • ReflectionClass::newInstance — Creates a new class instance from given arguments.

2.) ReflectionExtension — The ReflectionExtension class => PHP Extension Reflection

  • ReflectionExtension::getClasses — Gets classes
  • ReflectionExtension::getVersion — Gets extension version
  • ReflectionExtension::info — Gets extension info

3.) ReflectionFunction — The ReflectionFunction class => Function Reflection

  • ReflectionFunction::invoke — Invokes function

4.) ReflectionMethod — The ReflectionMethod class => Method Reflection

  • ReflectionMethod::getDeclaringClass — Gets declaring class for the reflected method.
  • ReflectionMethod::invoke — Invoke
  • ReflectionMethod::isAbstract — Checks if method is abstract
  • ReflectionMethod::isConstructor — Checks if method is a constructor
  • ReflectionMethod::isDestructor — Checks if method is a destructor
  • ReflectionMethod::isFinal — Checks if method is final
  • ReflectionMethod::isPrivate — Checks if method is private
  • ReflectionMethod::isProtected — Checks if method is protected
  • ReflectionMethod::isPublic — Checks if method is public
  • ReflectionMethod::isStatic — Checks if method is static

5.) ReflectionParameter — The ReflectionParameter class => Parameter Reflection

  • ReflectionParameter::getName — Gets parameter name
  • ReflectionParameter::isArray — Checks if parameter expects an array
  • ReflectionParameter::isDefaultValueAvailable — Checks if a default value is available
  • ReflectionParameter::isOptional — Checks if optional
  • ReflectionParameter::isPassedByReference — Checks if passed by reference

6.) ReflectionProperty — The ReflectionProperty class => Property Reflection

  • ReflectionProperty::getDocComment — Gets doc comment
  • ReflectionProperty::getName — Gets property name
  • ReflectionProperty::isPrivate — Checks if property is private
  • ReflectionProperty::isProtected — Checks if property is protected
  • ReflectionProperty::isPublic — Checks if property is public
  • ReflectionProperty::isStatic — Checks if property is static
  • ReflectionProperty::setValue — Set property value

Zend bietet noch ein Abstraktionslayer dafür an.
Z.B. Direkt alle Methoden als Reflection-Objects und nicht als String zu bekommen.

Es lassen sich z.B. Properties befüllen.

Beispiel für die Verwendung:
[code]
getConstants())) {
throw new Facebook_Exception(‚Type of Object is incorrect‘);
}

$className = ‚Facebook_Object_‘ . $type;

if (!class_exists($className)) {
throw new Facebook_Exception(‚Class „‚ . $className . ‚“ could not be instantiated.‘);
}

return new $className($data);
}
}
[/code]

GD Star Rating
loading...

Kommentar verfassen