SlideShare a Scribd company logo
Mirror, Mirror on the Wall
James Titcumb
NomadPHP December 2015
James Titcumb
www.jamestitcumb.com
www.roave.com
www.phphants.co.uk
www.phpsouthcoast.co.uk
@asgrim
Who is this guy?
Reflection
Mirror, mirror on the wall (Nomad PHP US 2015)
© 1937 Disney’s Snow White - disneyscreencaps.com
Mirror, mirror on the wall (Nomad PHP US 2015)
● Structure
● Metadata
● Values
● Type introspection
● Modification
Reflection
How does it work?
GET_REFLECTION_OBJECT_PTR(ce);
lc_name = zend_str_tolower_dup(name, name_len);
if ((ce == zend_ce_closure && (name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1)
&& memcmp(lc_name, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1) == 0)
|| zend_hash_str_exists(&ce->function_table, lc_name, name_len)) {
efree(lc_name);
RETURN_TRUE;
} else {
efree(lc_name);
RETURN_FALSE;
}
Okay. What now?
github.com/ /BetterReflection
Better Reflection!
Mirror, mirror on the wall (Nomad PHP US 2015)
What?
Why?
How?
Magic of the AST
source: http://goo.gl/HORwLQ
WTF is AST?
use PhpParserParserFactory;
$parser = (new ParserFactory)
->create(ParserFactory::PREFER_PHP7);
print_r($parser->parse(
file_get_contents('ast-demo-src.php')
));
<?php
echo "Hello world";
AST
`-- Echo statement
`-- String, value "Hello world"
<?php
echo "Hello " . "world";
AST
`-- Echo statement
`-- Concat
|-- Left
| `-- String, value "Hello "
`-- Right
`-- String, value "world"
AST to Reflection
Benefits?
ReflectionProperty->getDocBlockTypes()
Types!
Using Better Reflection
$reflection = new ReflectionClass(
'BetterReflectionTestFixtureExampleClass'
);
$this->assertSame(
'ExampleClass',
$reflection->getShortName()
);
$reflection = ReflectionClass::createFromName(
'BetterReflectionTestFixtureExampleClass'
);
$this->assertSame(
'ExampleClass',
$reflection->getShortName()
);
// In ReflectionClass :
public static function createFromName($className)
{
return ClassReflector::buildDefaultReflector()->reflect($className);
}
// In ClassReflector :
public static function buildDefaultReflector()
{
return new self(new AggregateSourceLocator([
new PhpInternalSourceLocator(),
new EvaledCodeSourceLocator(),
new AutoloadSourceLocator(),
]));
}
(don’t try this at home kids!)
Some voodoo...
Mirror, mirror on the wall (Nomad PHP US 2015)
class MyClass
{
public function foo()
{
return 5;
}
}
// Create the reflection first
// ***BEFORE*** class is loaded
$classInfo = ReflectionClass::createFromName('MyClass');
// Override the body...!
$methodInfo = $classInfo->getMethod('foo');
$methodInfo->setBody(function () {
return 4;
});
// Save the class and require it
$classCode = (new CodePrinter())->prettyPrint([$classInfo->getAst()]);
$tmpFile = tempnam(sys_get_temp_dir(), 'br-monkey-patching');
file_put_contents($tmpFile, '<?php ' . $classCode);
require_once($tmpFile);
unlink($tmpFile);
// Now create an instance, and call the function...
$c = new MyClass();
var_dump($c->foo()); // will be 4!!!
Mirror, mirror on the wall (Nomad PHP US 2015)
What’s in it for me?
The future...
● DONE Currently: rewriting the internals
○ SourceLocators “might” return some code ?!
● DONE Returning AST/Code for method/fn
Ideas/plans - DONE
● WIP More compatibility core reflection
● WIP Reflect core fns with default params
○ This is not currently possible with core reflection
● WIP Modification & exporting
● WIP Reflecting on closures
Ideas/plans - WIP
● Adding a getColumn() function
● PHP 7 compatibility
● @inheritDoc traversal (to inherit type hints)
● Make it faster
Ideas/plans
¯_(ツ)_/¯
github.com/ /BetterReflection
Better Reflection
Any questions? :)
https://joind.in/16523
James Titcumb @asgrim

More Related Content

PDF
Mirror, mirror on the wall (PHPem Unconf 2015)
TXT
Divided difference Matlab code
DOCX
graficas matlab
PPTX
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
PDF
AST + Better Reflection (PHP Benelux 2016 Unconference)
PPTX
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
PDF
2010 jan 2
KEY
RNA Secondary Structure Prediction
Mirror, mirror on the wall (PHPem Unconf 2015)
Divided difference Matlab code
graficas matlab
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
AST + Better Reflection (PHP Benelux 2016 Unconference)
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
2010 jan 2
RNA Secondary Structure Prediction

Viewers also liked (20)

PPTX
Hubbard exteriors
PPTX
Responding to literature
PDF
How to launch a event ticketing sales business on mobile [Part 1 - Mobile Str...
PPT
Aula nefropatias agudas e cronicas 2
PPT
Marcelle poirier
PPTX
immunotherapy for multiple myeloma
PDF
Guy ros (1)
PDF
Michael Durante EBITDA Shortcomings
PPT
Marcelle Poirier is a Reputed Avocat Francophone in Miami
PDF
Informatica solidale - cooperazione internazionale 9 aprile 2015
PDF
Open source e cloud per il non profit - settembre 2016 -
PDF
Guy Ros dédicace son dernier livre
DOC
Trabalhoredes
PPT
1 orlowski targeted-therapies
PDF
TD SOBRE MOTORES
PPT
Nikolai angelov-gary-2015-1
PPT
Marlynne Grant researchED conf. 6th Sept 2014 - 30 minutes
PPTX
Slide show intro
PDF
Dip Your Toes in the Sea of Security (DPC 2015)
PPT
Casutadinoala
Hubbard exteriors
Responding to literature
How to launch a event ticketing sales business on mobile [Part 1 - Mobile Str...
Aula nefropatias agudas e cronicas 2
Marcelle poirier
immunotherapy for multiple myeloma
Guy ros (1)
Michael Durante EBITDA Shortcomings
Marcelle Poirier is a Reputed Avocat Francophone in Miami
Informatica solidale - cooperazione internazionale 9 aprile 2015
Open source e cloud per il non profit - settembre 2016 -
Guy Ros dédicace son dernier livre
Trabalhoredes
1 orlowski targeted-therapies
TD SOBRE MOTORES
Nikolai angelov-gary-2015-1
Marlynne Grant researchED conf. 6th Sept 2014 - 30 minutes
Slide show intro
Dip Your Toes in the Sea of Security (DPC 2015)
Casutadinoala
Ad

More from James Titcumb (20)

PDF
Living the Best Life on a Legacy Project (phpday 2022).pdf
PDF
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
PDF
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
PDF
Best practices for crafting high quality PHP apps (Bulgaria 2019)
PDF
Climbing the Abstract Syntax Tree (php[world] 2019)
PDF
Best practices for crafting high quality PHP apps (php[world] 2019)
PDF
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
PDF
Climbing the Abstract Syntax Tree (PHP Russia 2019)
PDF
Best practices for crafting high quality PHP apps - PHP UK 2019
PDF
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
PDF
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
PDF
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
PDF
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
PDF
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
PDF
Crafting Quality PHP Applications (PHPkonf 2018)
PDF
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
PDF
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
PDF
Climbing the Abstract Syntax Tree (PHP UK 2018)
Living the Best Life on a Legacy Project (phpday 2022).pdf
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Best practices for crafting high quality PHP apps (Bulgaria 2019)
Climbing the Abstract Syntax Tree (php[world] 2019)
Best practices for crafting high quality PHP apps (php[world] 2019)
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)
Best practices for crafting high quality PHP apps - PHP UK 2019
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Best practices for crafting high quality PHP apps (PHP South Africa 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Crafting Quality PHP Applications (PHPkonf 2018)
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)
Ad

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Big Data Technologies - Introduction.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
cuic standard and advanced reporting.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Spectral efficient network and resource selection model in 5G networks
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25 Week I
MYSQL Presentation for SQL database connectivity
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine learning based COVID-19 study performance prediction
Programs and apps: productivity, graphics, security and other tools
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Review of recent advances in non-invasive hemoglobin estimation
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”

Mirror, mirror on the wall (Nomad PHP US 2015)