Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf ·...

41
Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion Performing Magic with PHP Metaprogramming and Reflection Gregor Gabrysiak Stefan Marr Falko Menge Hasso Plattner Institute University of Potsdam 15 January 2008 Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 1 / 36

Transcript of Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf ·...

Page 1: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Performing Magic with PHPMetaprogramming and Reflection

Gregor Gabrysiak Stefan Marr Falko Menge

Hasso Plattner InstituteUniversity of Potsdam

15 January 2008

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 1 / 36

Page 2: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

We all know what PHP is...

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 2 / 36

Page 3: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

We all know what PHP is...

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 2 / 36

Page 4: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

We all know what PHP is...

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 2 / 36

Page 5: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

We all know what PHP is...

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 2 / 36

Page 6: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

We all know what PHP is...

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 2 / 36

Page 7: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

We all know what PHP is...

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 2 / 36

Page 8: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

History of PHP

Timeline1995 "Personal Home Page Tools"

Developed by Rasmus Lerdorf1996 PHP: Hypertext Preprocessor Version 2

Already used by approx. 1% of all domains (in 1997)1998 PHP 3

Available on 10% of all Web servers (1998)2000 PHP 4

Zend Engine released, first rudimentary object model2004 PHP 5

New object model, improved XML, database abstractionPHP 6 is on the way

Unicode support, legacy features dropped

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 3 / 36

Page 9: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Application Domains of PHP

Server-side scripting - Traditional domain of PHPCommand-line scripting, partly replacing Perl, Python,awk, or shell scriptingphp -r "sleep(7*60); echo chr(7);"

Bindings to GUI libraries, such as GTK+, Qt and Win32

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 4 / 36

Page 10: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Outline

1 Language Features of PHPObject model

Dynamic object propertiesMagic methods

2 PHP M&R BasicsPHP5 Reflection APIReflection in actionBasic intercession

3 PHP Extensions for M&ReZ Components: Reflection ComponentRunkit

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 5 / 36

Page 11: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Language Features of PHP

Object ModelDynamically typedPHP4 has already offered an object model

Objects were handled like primitive typesPassed by value

PHP5 has reintroduced classes and interfacesObjects are passed by referenceVisibilitiesModifiers: static, final, abstract

Garbage collection basically using reference countingType hinting

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 6 / 36

Page 12: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

interface iDog {public function setName($name);public function sit();

}abstract class Dog implements iDog {

protected $name = "unnamed dog";public function setName($name) {

$this->name = $name;}final public function sit() {

echo "$this->name sat down, barking: ";echo $this->bark();

}protected function bark() {

print "WOOF";}

}class Poodle extends Dog implements iDog {

final protected function bark() {echo " wiif ";

}}$b = new Poodle();$b->setName("small poodle");$b->sit();

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 7 / 36

Page 13: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Magic Methods

Dynamic Object Properties__get(), __set()

Called when property to get or to set is not availableMay be overloaded to enforce different behavior

__isset(): tests whether a variable is set or nil__unset(): destroys the values of certain properties__clone(): defines how an instance is to be duplicated

__toString(): describes a class’ string representation__sleep(), __wakeup(), __set_state()

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 8 / 36

Page 14: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Magic Methods

__autoload()Used to load all necessary classes onceSaves parse time and memory

__call()Captures the invocation of non-existing methodsBehavior can be adjusted for every classSimilar to Smalltalk’s #doesNotUnderstand:aMessage

class s {private $default = "default answer";public function __call($name, $arguments) {

echo "Method $name called:\n";var_dump($arguments);return $this->default;

}}

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 9 / 36

Page 15: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

1 Language Features of PHPObject model

Dynamic object propertiesMagic methods

2 PHP M&R BasicsPHP5 Reflection APIReflection in actionBasic intercession

3 PHP Extensions for M&ReZ Components: Reflection ComponentRunkit

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 10 / 36

Page 16: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

+getName() : string+isPublic() : bool+isPrivate() : bool+isProtected() : bool+isStatic() : bool+isDefault() : bool+getModifiers() : int+getValue(ein object : object) : mixed+setValue(ein object : object, ein value : mixed)+getDeclaringClass() : ReflectionClass+getDocComment() : string

ReflectionProperty

+getName() : string+isInternal() : bool+isUserDefined() : bool+isInstantiable() : bool+hasConstant(ein name : string) : bool+hasMethod(ein name : string) : bool+hasProperty(ein name : string) : bool+getFileName() : string+getStartLine() : int+getEndLine() : int+getDocComment() : string+getConstructor() : ReflectionMethod+getMethod(ein name : string) : ReflectionMethod+getMethods() : ReflectionMethod[]+getProperty(ein name : string) : ReflectionProperty+getProperties() : ReflectionProperty[]+getConstants() : array+getConstant(ein name : string) : mixed+getInterfaces() : ReflectionClass[]+isInterface() : bool+isAbstract() : bool+isFinal() : bool+getModifiers() : int+isInstance(ein object : object) : bool+newInstance(ein args : mixed) : object+newInstanceArgs(ein args : array) : object+getParentClass() : ReflectionClass+isSubclassOf(ein class : ReflectionClass) : bool+getStaticProperties() : array+getStaticPropertyValue(ein name : string, ein default : mixed = null) : mixed+setStaticPropertyValue(ein name : string, ein value : mixed)+getDefaultProperties() : array+isIterateable() : bool+implementsInterfaces(ein name : string) : bool+getExtension() : ReflectionExtension+getExtensionName() : string

ReflectionClass

ReflectionObject

+invoke(ein object : object, ein ... : mixed) : mixed+invokeArgs(ein object : object, ein args : mixed) : mixed+isFinal() : bool+isAbstract() : bool+isPublic() : bool+isPrivate() : bool+isProtected() : bool+isStatic() : bool+isConstructor() : bool+isDestructor() : bool+getModifiers() : int+getDeclaringClass() : ReflectionClass

ReflectionMethod

+getName() : string+isPassedByReference() : bool+getDeclaringClass() : ReflectionClass+getClass() : ReflectionClass+isArray() : bool+allowsNull() : bool+isPassedByReference() : bool+isOptional() : bool+isDefaultValueAvailable() : bool+getDefaultValue() : mixed

ReflectionParameter

+getName() : string+getVersion() : string+getFunctions() : ReflectionFunction[]+getConstants() : array+getINIEntries() : array+getClasses() : ReflectionClass[]+getClassNames() : array+info() : string

ReflectionExtension

func_get_arg()func_get_args()func_num_args()is_subclass_of()get_parent_class()get_class()get_class_methods()get_object_vars()get_class_vars()class_exists()interface_exists()method_exists()property_exists()get_declared_classes()get_declared_interfaces()get_included_files()get_required_files()ini_set()ini_get()get_defined_constants()get_defined_functions()get_defined_vars()

<<functions>>+getName() : string+isInternal() : bool+isDisabled() : bool+isUserDefined() : bool+getFileName() : string+getStartLine() : int+getEndLine() : int+getDocComment() : string+getStaticVariables() : array+invoke(ein args : mixed, ein ... : mixed) : mixed+invokeArgs() : mixed+returnsReference() : bool+getParameters() : ReflectionParameter[]+getNumberOfParameters() : int+getNumberOfRequiredParameters() : int

ReflectionFunction

Page 17: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

PHP5 Reflection API

ReflectionPropertyReflectionClass

ReflectionObject

ReflectionMethod

ReflectionParameter

ReflectionExtension

func_get_arg()func_get_args()func_num_args()is_subclass_of()get_parent_class()get_class()get_class_methods()get_object_vars()get_class_vars()class_exists()interface_exists()method_exists()property_exists()get_declared_classes()get_declared_interfaces()get_included_files()get_required_files()ini_set()ini_get()get_defined_constants()get_defined_functions()get_defined_vars()

<<functions>>

ReflectionFunction

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 12 / 36

Page 18: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

PHP5 Reflection API

ReflectionPropertyReflectionClass

ReflectionObject

ReflectionMethod

ReflectionParameter

ReflectionExtension

func_get_arg()func_get_args()func_num_args()is_subclass_of()get_parent_class()get_class()get_class_methods()get_object_vars()get_class_vars()class_exists()interface_exists()method_exists()property_exists()get_declared_classes()get_declared_interfaces()get_included_files()get_required_files()ini_set()ini_get()get_defined_constants()get_defined_functions()get_defined_vars()

<<functions>>

ReflectionFunction

PHP System Runtime Data

ReflectionPHP Core

Classes Objects ...

PH

P V

irtua

l Mac

hine

with

Sys

tem

Lev

el E

xten

sion

s

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 13 / 36

Page 19: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

PHP5 Reflection API

Wraps underlying VM data structuresProvides introspection and method/function invocationcapabilitiesCan be deactivated (to reduce VM memory footprint)Respects the Mirrors design principle [1] to a high degree

ReflectionPropertyReflectionClass

ReflectionObject

ReflectionMethod

ReflectionParameter

ReflectionExtension

func_get_arg()func_get_args()func_num_args()is_subclass_of()get_parent_class()get_class()get_class_methods()get_object_vars()get_class_vars()class_exists()interface_exists()method_exists()property_exists()get_declared_classes()get_declared_interfaces()get_included_files()get_required_files()ini_set()ini_get()get_defined_constants()get_defined_functions()get_defined_vars()

<<functions>>

ReflectionFunction

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 14 / 36

Page 20: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Reflection in Action

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 15 / 36

Page 21: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Reflection in Action: debug_backtrace

class DebugBacktrace {public function callee() {

echo ’Caller of this Function was ’,DebugBacktrace::getCaller(), ".\n";

}public static function getCaller() {

$trace = debug_backtrace();return $trace[2][’class’] . $trace[2][’type’]

. $trace[2][’function’];}public function test1() {

$this->callee();}public static function test2($t) {

$t->callee();}

}

$d = new DebugBacktrace();$d->test1(1, 2, 3);DebugBacktrace::test2($d);

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 16 / 36

Page 22: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Common Usage of Reflection in PHP

XML/JSON to PHP mappingFrameworks for dependency injection and AOPPHPCallGraph –> visualizationPHPUnit: Mockup objects, generation of test skeletons,code metrics

Example for Annotations

class Calculator {/*** @assert (0, 0) == 0

* @assert (0, 1) == 1

* @assert (1, 1) == 2

*/public function add($a, $b) { return $a + $b; }

}

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 17 / 36

Page 23: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Basic Intercession

Execute Generated Codeeval()

create_function()

Dynamically Call a Method or Functioncall_user_function()

call_user_function_array()

call_user_method()

call_user_method_array()

$functionName($a, $b);

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 18 / 36

Page 24: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Basic Intercession: create_function

create_function

$func = create_function(’$foo,$bar’, ’echo "CreatedFunction:$foo $bar\n";’);

$func(’Hello’, ’World!’);var_dump(addslashes($func));

call_user_func($func, ’FOO’, ’baz’);

//common usage example$av = array("the ", "a ", "that ", "this ");array_walk($av, create_function(’&$v,$k’, ’$v = $v . "mango";’))

;print_r($av);

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 19 / 36

Page 25: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Basic Intercession: Invoke Methods

Invoke Methods

class InvokeTest {public function callMe() {

echo "Yeah, you called me!\n";}

}

$o = new InvokeTest();$class = new ReflectionClass($o);$method = $class->getMethod(’callMe’);$method->invoke($o, ’callMe’);

function fooFunc($bar) {echo "$bar\n";

}

$func = ’fooFunc’;$func(’Hello World’);

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 20 / 36

Page 26: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

1 Language Features of PHPObject model

Dynamic object propertiesMagic methods

2 PHP M&R BasicsPHP5 Reflection APIReflection in actionBasic intercession

3 PHP Extensions for M&ReZ Components: Reflection ComponentRunkit

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 21 / 36

Page 27: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Annotations in PHP

Class with Type Annotations

class HelloWorld {

/*** @var string

*/private $hello = ’Hello’;

/*** @param string $name

* @return string

*/public function getGreeting($name) {

return "$this->hello $name!";}

}

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 22 / 36

Page 28: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

eZ Components: Reflection Component

ezcReflectionProperty ezcReflectionClass ezcReflectionMethod ezcReflectionParameter

ReflectionProperty ReflectionClass ReflectionMethod ReflectionParameter

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 23 / 36

Page 29: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

eZ Components: Reflection Component

+getAnnotations()+hasAnnotation()+getType()

ezcReflectionProperty

+getAnnotations()+hasAnnotation()

ezcReflectionClass

+getAnnotations()+hasAnnotation()+getReturnType()+isMagic()+isInherited()+isOveridden()+isIntroduced()

ezcReflectionMethod

+getType()

ezcReflectionParameter

ReflectionProperty ReflectionClass ReflectionMethod ReflectionParameter

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 24 / 36

Page 30: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

eZ Components: Reflection Component

PHP Core Functions Reflection API

PHP System Runtime DataClasses Objects ...

PH

P V

M

ezcReflection

+getAnnotations()+hasAnnotation()+getType()

ezcReflectionProperty+getAnnotations()+hasAnnotation()

ezcReflectionClass+getAnnotations()+hasAnnotation()+getReturnType()+isMagic()+isInherited()+isOveridden()+isIntroduced()

ezcReflectionMethod+getType()ezcReflectionParameter

ezcReflectionClassType

«interface»ezcReflectionType

ezcReflectionAbstractType

ezcReflectionPrimitiveType ezcReflectionArrayType

ReflectionProperty ReflectionClass ReflectionMethod ReflectionParameter

PHP

Lang

uage

Lev

el Im

plem

enta

tions

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 25 / 36

Page 31: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

eZ Components: Reflection Component

PHP Core Functions Reflection API

PHP System Runtime DataClasses Objects ...

PH

P V

M

ezcReflection

+getAnnotations()+hasAnnotation()+getType()

ezcReflectionProperty+getAnnotations()+hasAnnotation()

ezcReflectionClass+getAnnotations()+hasAnnotation()+getReturnType()+isMagic()+isInherited()+isOveridden()+isIntroduced()

ezcReflectionMethod+getType()ezcReflectionParameter

ezcReflectionClassType

«interface»ezcReflectionType

ezcReflectionAbstractType

ezcReflectionPrimitiveType ezcReflectionArrayType

ReflectionProperty ReflectionClass ReflectionMethod ReflectionParameter

PHP

Lang

uage

Lev

el Im

plem

enta

tions

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 26 / 36

Page 32: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

eZ Components: Reflection Component

Feature SummaryIntroduces support for PHPDoc-style annotationsType annotations are accessible at runtime forintrospectionNo runtime constraintsCan leverage other reflection implementations as a datasource, e.g. StaticReflectionUsed e.g. for WSDL generation

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 27 / 36

Page 33: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Annotations for WSDL Generation

Hello World Web Service

/*** @webservice

*/class HelloWorld {

/*** @param string $name

* @return string

* @webmethod

*/public function getGreeting($name) {

return "Hello $name!";}

}

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 28 / 36

Page 34: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Doing Real Intercession: The Runkit Extension

Features of RunkitModify functions and methods

add, copy, redefine, rename and removeModify constants and class constants

add, redefine and removeModify the inheritance structure

adopt and emancipate classes

Check if a return value will be usedSandbox classesLint functions

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 29 / 36

Page 35: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Doing Real Intercession: The Runkit Extension

func_get_arg()func_get_args()func_num_args()is_subclass_of()get_parent_class()get_class()get_class_methods()get_object_vars()get_class_vars()class_exists()interface_exists()method_exists()property_exists()get_declared_classes()get_declared_interfaces()get_included_files()get_required_files()ini_set()ini_get()get_defined_constants()get_defined_functions()get_defined_vars()

<<functions>>

PHP System Runtime Data

ReflectionPHP Core

Classes Objects ...

PH

P V

irtu

al M

ach

ine

with

Sys

tem

Le

vel E

xte

nsi

ons

runkit_class_adopt()runkit_class_emancipate()runkit_constant_add()runkit_constant_redefine()runkit_constant_remove()runkit_function_add()runkit_function_copy()runkit_function_redefine()runkit_function_remove()runkit_function_rename()runkit_import()runkit_lint_file()runkit_lint()runkit_method_add()runkit_method_copy()runkit_method_redefine()runkit_method_remove()runkit_method_rename()runkit_return_value_used()runkit_sandbox_output_handler()runkit_superglobals()

<<functions>>

Runkit

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 30 / 36

Page 36: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Object-oriented Runkit

PHP Core Functions

PHP System Runtime DataClasses Objects ...

PHP

VM

isvcRunkit

+emancipate()+setParentClass()+addNewMethod()+addExistingMethod()+removeMethod()

isvcRunkitClass+getCode()+setCode()+delete()+setName()

isvcRunkitMethod

ReflectionClass ReflectionMethod

Lang

uage

Lev

el Im

plem

enta

tions

Reflection API Runkit Extension

ezcReflection

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 31 / 36

Page 37: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Other Metaprogramming Extensions for PHP

Advanced PHP Debugger (APD)override_function()rename_function()Similar features provided by Runkit

Tokenizer and parse_treeNot reflective but very helpful for metaprogramming

Several aspect-oriented programming (AOP) frameworksStatic aspect weavingUsing PHPDoc-style annotations and sometimes XML

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 32 / 36

Page 38: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Language Features of PHP PHP M&R Basics PHP Extensions for M&R Conclusion

Conclusion

PHP Core Functions

PHP System Runtime DataClasses Objects ...

PHP

VM

isvcRunkit

PHP

Cod

e

Reflection API Runkit Extension

ezcReflection

PHP has many reflective featuresMOP divided into object-oriented and procedural APIAdditional features provided by extensions

Main use case: Web applicationsExtensive intercession makes not much sensefor the usual PHP execution modelIn general more useful in long running programs,e.g. GTK+, QT, MFC or command line applications

[6][4][3][2][5]Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 33 / 36

Page 39: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Appendix

For Further Reading I

Gilad Bracha and David Ungar.Mirrors: design principles for meta-level facilities ofobject-oriented programming languages.In OOPSLA ’04: Proceedings of the 19th annual ACMSIGPLAN conference on Object-oriented programming,systems, languages, and applications, pages 331–344,New York, NY, USA, 2004. ACM.

Sara Golemon.Runkit.PHP Extension, The PHP Group, January 2008.http://pecl.php.net/package/runkit.

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 34 / 36

Page 40: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Appendix

For Further Reading II

Stefan Marr and Falko Menge.ezcReflection.SVN Repository, eZ Components, January 2008.http://svn.ez.no/svn/ezcomponents/experimental/Reflection.

PHP Documentation Group.PHP Manual.Documentation, November 2007.http://www.php.net/manual/en/language.oop5.reflection.php.

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 35 / 36

Page 41: Performing Magic with PHPinstantsvc.sourceforge.net/docs/...and-reflection-with-php-v1.0.pdf · 1995 "Personal Home Page Tools" Developed by Rasmus Lerdorf 1996 PHP: Hypertext Preprocessor

Appendix

For Further Reading III

The PHP Group.PHP Extension Community Library.Project Site, January 2008.http://pecl.php.net/.

David Welton.Programming Language Popularity.Web Site, DedaSys LLC, 2008.http://www.langpop.com/.

Gregor Gabrysiak, Stefan Marr, Falko Menge Metaprogramming and Reflection 36 / 36