SlideShare a Scribd company logo
1 of 81
Download to read offline
PHP
Under The Hood
What Is PHP?
● An Easy To Learn Language?
○ Sure!
● A Simple Language?
○ Usually
● A Well Designed Language?
○ HA!
● An Effective Language!
○ Absolutely!
PHP Is...
Dynamically
Typed
$a = 1;
$b = "a";
PHP Is...
Weak Typed
1 + "1b" == 2
PHP Is...
Implementation
Defined
Implementations
Main:
C-PHP: www.php.net
Alt:
HipHop - Facebook
Roadsend - Compiler
Phalanger - .NET
PHP Compiler - Compiler
PHPPHP - PHP
Interesting Note:
C-PHP Requires
PHP To Compile!
PHP Is...
Compiled
PHP Is...
Dynamically
Compiled
On-Demand
It All Starts With The
SAPI
SAPI
● Server API
● "Starts" PHP and issues request(s)
● Common SAPIs:
○ CLI
○ CGI (and FastCGI)
○ mod_php
○ FPM
○ etc...
Execution Pipeline
Execution Pipeline
Start Up
Request
Shut Down
SAPI
Execution Pipeline
Start Up
Request
Shut Down
SAPI
Config Init
Engine Init
Module Init
Zend/zend.c
PHPPHP/PHP.php
Execution Pipeline
Start Up
Request
Shut Down
SAPI
Module
Shutdown
Engine
Shutdown
Zend/zend.c
Execution Pipeline
Start Up
Request
Shut Down
SAPI
Request Init
Compile Code
Execute Code
Request
Shutdown
Compiler Pipeline
Lexer
Lexer Step
● C-PHP
○ Tokenizes using RE2C
○ Produces Array of Tokens
● PHPPHP
○ Uses core tokenizer (or emulative)
○ PHPParser Powered
Zend/zend_language_scanner.l
PHPParser/Lexer/Emulative.php
Compiler Pipeline
Lexer
Parser
Parse Step
● C-PHP
○ Directly Generates OPCode Array
○ BISON Powered
● PHPPHP
○ Generates AST Structure
○ PHPParser Powered
Zend/zend_language_parser.y
Compiler Pipeline
Lexer
Parser
Compiler
Compile Step
● C-PHP
○ Compiled Directly During Parse
○ Single Pass Compilation
● PHPPHP
○ Recurses Over AST
○ Single Pass (for now)
○
PHPPHP/Engine/Compiler.php
Compiler Pipeline
Lexer
Parser
Compiler
OpCode
OpCodes
● A Series of Operations
○ Just like Assembler Opcodes
○ Represent "units of functionality"
● Designed to run on Virtual Machine
○ Zend Engine
○ Or PHPPHP!
$a = 1;
$b = 2;
echo $a + $b;
Notice Anything?
What If We
Cached The
OpCodes?
We Can Cache!
● Given the compiler is Idempodent
○ (has no side-effects)
○ (hint: it's not)
● OpCodes are really Pointers
○ Swizzling!!!
In Other Words
OpCode Caching
Is Hard!
Time To Execute!
Zend/zend_vm_execute.h
PHPPHP/Engine/Executor.php
Executor Pipeline
OpCode
Is
Return?No Yes
Return
But What Are We
Executing?
Zend/zend_vm_execute.h
Interesting Note:
vm_execute.h
Is Generated By
PHP
PHPPHP/Engine/OpLines/Add.php
Variables!
Zend/zend.h
PHPPHP/Engine/Zval/Value.php
Ref-Counting
● RefCount + References
○ Allows Copy-On-Write
● Variable Is "Deleted" When
RefCount = 0
● Enables Primitive Garbage
Collection
○ Circular GC is also implemented
That's All There Is
To It!
Let's Look At An
Example
$a = 1;
$b = 2;
var_dump(
$a + $b
);
line # op return operands
-----------------------------------
2 0 ASSIGN !0, 1
3 1 ASSIGN !1, 2
6 2 ADD ~2 !0, !1
3 SEND_VAL ~2
4 DO_FCALL 'var_dump'
5 RETURN 1
[0] => PHPPHPEngineOpLinesAssign
[1] => PHPPHPEngineOpLinesAssign
[2] => PHPPHPEngineOpLinesAdd
[3] => PHPPHPEngineOpLinesInitFCallByName
[4] => PHPPHPEngineOpLinesSend
[5] => PHPPHPEngineOpLinesFunctionCall
[6] => PHPPHPEngineOpLinesReturnOp
[0] => PHPPHPEngineOpLinesAssign Object (
[op1] => PHPPHPEngineZvalPtr Object (
[zval:protected] => PHPPHPEngineZvalVariable Object (
[name:protected] => PHPPHPEngineZvalPtr Object (
[zval:protected] => PHPPHPEngineZvalValue Object (
[value:protected] => a
[refcount:protected] => 1
[isRef:protected] =>
[dtorFunc:protected] =>
)
)
[class:protected] =>
[zval:protected] =>
[executor:protected] =>
[scope] => 1
)
)
[op2] => PHPPHPEngineZvalPtr Object (
[zval:protected] => PHPPHPEngineZvalValue Object (
[value:protected] => 1
[refcount:protected] => 1
[isRef:protected] =>
[dtorFunc:protected] =>
)
)
[result] =>
[lineno] => 2
)
PHPPHP/Engine/OpLines/Assign.php
PHPPHP/Engine/OpLines/Add.php
PHPPHP/Engine/OpLines/InitFCallByName.php
PHPPHP/Engine/OpLines/Send.php
PHPPHP/Engine/OpLines/FunctionCall.php
There's A Ton
More
Get Involved!
More Info
● github.com/php/php-src
● lxr.php.net
● github.com/ircmaxell/PHPPHP
● Reference Series
○ wiki.php.net
○ blog.ircmaxell.com
■ PHP Internals Series
Anthony Ferrara
Joind.in/8443
@ircmaxell
blog.ircmaxell.com
ircmaxell@php.net
youtube.com/ircmaxell

More Related Content

What's hot

What's hot (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Clean code
Clean codeClean code
Clean code
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
 
Q2.12: Debugging with GDB
Q2.12: Debugging with GDBQ2.12: Debugging with GDB
Q2.12: Debugging with GDB
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphism
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDB
 
Goroutines and Channels in practice
Goroutines and Channels in practiceGoroutines and Channels in practice
Goroutines and Channels in practice
 
Loops in C
Loops in CLoops in C
Loops in C
 
Sigreturn Oriented Programming
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented Programming
 
Memory in go
Memory in goMemory in go
Memory in go
 
An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
【学習メモ#1st】12ステップで作る組込みOS自作入門
【学習メモ#1st】12ステップで作る組込みOS自作入門【学習メモ#1st】12ステップで作る組込みOS自作入門
【学習メモ#1st】12ステップで作る組込みOS自作入門
 
Javascript this keyword
Javascript this keywordJavascript this keyword
Javascript this keyword
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Seven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many ProgrammersSeven Ineffective Coding Habits of Many Programmers
Seven Ineffective Coding Habits of Many Programmers
 
Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)Windows 10 Nt Heap Exploitation (English version)
Windows 10 Nt Heap Exploitation (English version)
 
The Rust Programming Language: an Overview
The Rust Programming Language: an OverviewThe Rust Programming Language: an Overview
The Rust Programming Language: an Overview
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Debugging Applications with GNU Debugger
Debugging Applications with GNU DebuggerDebugging Applications with GNU Debugger
Debugging Applications with GNU Debugger
 

Viewers also liked

Viewers also liked (13)

PHP 7 new engine
PHP 7 new enginePHP 7 new engine
PHP 7 new engine
 
Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)Being functional in PHP (PHPDay Italy 2016)
Being functional in PHP (PHPDay Italy 2016)
 
Php
PhpPhp
Php
 
Internet of Things With PHP
Internet of Things With PHPInternet of Things With PHP
Internet of Things With PHP
 
PHP Optimization
PHP OptimizationPHP Optimization
PHP Optimization
 
PHP WTF
PHP WTFPHP WTF
PHP WTF
 
PHP 7 performances from PHP 5
PHP 7 performances from PHP 5PHP 7 performances from PHP 5
PHP 7 performances from PHP 5
 
Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
 
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
[Community Open Camp] 給 PHP 開發者的 VS Code 指南[Community Open Camp] 給 PHP 開發者的 VS Code 指南
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
 
How PHP Works ?
How PHP Works ?How PHP Works ?
How PHP Works ?
 
LaravelConf Taiwan 2017 開幕
LaravelConf Taiwan 2017 開幕LaravelConf Taiwan 2017 開幕
LaravelConf Taiwan 2017 開幕
 
Route 路由控制
Route 路由控制Route 路由控制
Route 路由控制
 

Similar to PHP, Under The Hood - DPC

Php under the_hood
Php under the_hoodPhp under the_hood
Php under the_hood
frank_neff
 
Php4android TDC 2011
Php4android TDC 2011Php4android TDC 2011
Php4android TDC 2011
Kinn Julião
 

Similar to PHP, Under The Hood - DPC (20)

IPC 2013 - High Performance PHP with HipHop
IPC 2013 - High Performance PHP with HipHopIPC 2013 - High Performance PHP with HipHop
IPC 2013 - High Performance PHP with HipHop
 
Laravel level 0 (introduction)
Laravel level 0 (introduction)Laravel level 0 (introduction)
Laravel level 0 (introduction)
 
Introduction to PHP (SDPHP)
Introduction to PHP   (SDPHP)Introduction to PHP   (SDPHP)
Introduction to PHP (SDPHP)
 
Php under the_hood
Php under the_hoodPhp under the_hood
Php under the_hood
 
Hiphop - PHP
Hiphop - PHPHiphop - PHP
Hiphop - PHP
 
AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8
 
Daniel Sloof: Magento on HHVM
Daniel Sloof: Magento on HHVMDaniel Sloof: Magento on HHVM
Daniel Sloof: Magento on HHVM
 
How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016
How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016
How Badoo Saved $1M Switching to PHP7 - Nikolay Krapivnyy - PHPDay Verona 2016
 
Go fundamentals
Go fundamentalsGo fundamentals
Go fundamentals
 
Making CLIs with Node.js
Making CLIs with Node.jsMaking CLIs with Node.js
Making CLIs with Node.js
 
Powering Tensorflow with big data using Apache Beam, Flink, and Spark - OSCON...
Powering Tensorflow with big data using Apache Beam, Flink, and Spark - OSCON...Powering Tensorflow with big data using Apache Beam, Flink, and Spark - OSCON...
Powering Tensorflow with big data using Apache Beam, Flink, and Spark - OSCON...
 
Debugging Drupal with Xdebug
Debugging Drupal with XdebugDebugging Drupal with Xdebug
Debugging Drupal with Xdebug
 
Introduction to PHP - SDPHP
Introduction to PHP - SDPHPIntroduction to PHP - SDPHP
Introduction to PHP - SDPHP
 
Advanced debugging techniques (PHP)
Advanced debugging techniques (PHP)Advanced debugging techniques (PHP)
Advanced debugging techniques (PHP)
 
Ruxmon.2015-08.-.proxenet
Ruxmon.2015-08.-.proxenetRuxmon.2015-08.-.proxenet
Ruxmon.2015-08.-.proxenet
 
Magento on HHVM. Daniel Sloof
Magento on HHVM. Daniel SloofMagento on HHVM. Daniel Sloof
Magento on HHVM. Daniel Sloof
 
Php4android TDC 2011
Php4android TDC 2011Php4android TDC 2011
Php4android TDC 2011
 
Swoole Meetup AFUP¨Montpellier 27/01/2021
Swoole   Meetup  AFUP¨Montpellier 27/01/2021Swoole   Meetup  AFUP¨Montpellier 27/01/2021
Swoole Meetup AFUP¨Montpellier 27/01/2021
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
PHP vs Node.js
PHP vs Node.jsPHP vs Node.js
PHP vs Node.js
 

More from Anthony Ferrara

More from Anthony Ferrara (11)

Password Storage And Attacking In PHP - PHP Argentina
Password Storage And Attacking In PHP - PHP ArgentinaPassword Storage And Attacking In PHP - PHP Argentina
Password Storage And Attacking In PHP - PHP Argentina
 
Beyond design patterns phpnw14
Beyond design patterns   phpnw14Beyond design patterns   phpnw14
Beyond design patterns phpnw14
 
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo EditionDon't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
 
Development By The Numbers - ConFoo Edition
Development By The Numbers - ConFoo EditionDevelopment By The Numbers - ConFoo Edition
Development By The Numbers - ConFoo Edition
 
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Don't Be STUPID, Grasp SOLID - DrupalCon PragueDon't Be STUPID, Grasp SOLID - DrupalCon Prague
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
 
Don't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHPDon't be STUPID, Grasp SOLID - North East PHP
Don't be STUPID, Grasp SOLID - North East PHP
 
Development by the numbers
Development by the numbersDevelopment by the numbers
Development by the numbers
 
Don't Be Stupid, Grasp Solid - MidWestPHP
Don't Be Stupid, Grasp Solid - MidWestPHPDon't Be Stupid, Grasp Solid - MidWestPHP
Don't Be Stupid, Grasp Solid - MidWestPHP
 
Cryptography For The Average Developer - Sunshine PHP
Cryptography For The Average Developer - Sunshine PHPCryptography For The Average Developer - Sunshine PHP
Cryptography For The Average Developer - Sunshine PHP
 
Password Storage and Attacking in PHP
Password Storage and Attacking in PHPPassword Storage and Attacking in PHP
Password Storage and Attacking in PHP
 
Cryptography For The Average Developer
Cryptography For The Average DeveloperCryptography For The Average Developer
Cryptography For The Average Developer
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

PHP, Under The Hood - DPC