PHP, Under The Hood - DPC
Upcoming SlideShare
Loading in...5

PHP, Under The Hood - DPC



PHP is one of the most popular open source programming languages in the world. It powers some of the highest traffic sites in the world, and at the same time it powers some of the lowest traffic sites ...

PHP is one of the most popular open source programming languages in the world. It powers some of the highest traffic sites in the world, and at the same time it powers some of the lowest traffic sites in the world. But have you ever wondered how it works under the hood? Have you been overwelmed by the thought of looking at the C code that runs PHP? Well, this talk is for you!

We're going to explore how PHP works under the hood, by looking at a PHP implementation of it: PHPPHP! Have you ever wondered what an OPCODE Cache is really doing? Have you ever wondered what a T_PAAMAYIM_NEKUDOTAYIM is? Have you ever wondered why an interpreted languages has a compiler? We'll explore all of these topics, and more! And the best part of it all? You don't need to know C to understand the details! Using PHPPHP, we can explore the language details in a high level language, where things like memory management don't get in the way of the real content. If you've ever wanted to know how PHP works, this is the talk for you!



Total Views
Views on SlideShare
Embed Views



11 Embeds 1,923 1849 26 25 7 6 4 2 1 1 1 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.


11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • post more
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    PHP, Under The Hood - DPC PHP, Under The Hood - DPC Presentation Transcript

    • PHPUnder 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...DynamicallyTyped
    • $a = 1;$b = "a";
    • PHP Is...Weak Typed
    • 1 + "1b" == 2
    • PHP Is...ImplementationDefined
    • ImplementationsMain:C-PHP: www.php.netAlt:HipHop - FacebookRoadsend - CompilerPhalanger - .NETPHP Compiler - CompilerPHPPHP - PHP
    • Interesting Note:C-PHP RequiresPHP To Compile!
    • PHP Is...Compiled
    • PHP Is...DynamicallyCompiledOn-Demand
    • It All Starts With TheSAPI
    • SAPI● Server API● "Starts" PHP and issues request(s)● Common SAPIs:○ CLI○ CGI (and FastCGI)○ mod_php○ FPM○ etc...
    • Execution Pipeline
    • Execution PipelineStart UpRequestShut DownSAPI
    • Execution PipelineStart UpRequestShut DownSAPIConfig InitEngine InitModule Init
    • Zend/zend.c
    • PHPPHP/PHP.php
    • Execution PipelineStart UpRequestShut DownSAPIModuleShutdownEngineShutdown
    • Zend/zend.c
    • Execution PipelineStart UpRequestShut DownSAPIRequest InitCompile CodeExecute CodeRequestShutdown
    • Compiler PipelineLexer
    • 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 PipelineLexerParser
    • Parse Step● C-PHP○ Directly Generates OPCode Array○ BISON Powered● PHPPHP○ Generates AST Structure○ PHPParser Powered
    • Zend/zend_language_parser.y
    • Compiler PipelineLexerParserCompiler
    • Compile Step● C-PHP○ Compiled Directly During Parse○ Single Pass Compilation● PHPPHP○ Recurses Over AST○ Single Pass (for now)○
    • PHPPHP/Engine/Compiler.php
    • Compiler PipelineLexerParserCompilerOpCode
    • 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 WeCached TheOpCodes?
    • We Can Cache!● Given the compiler is Idempodent○ (has no side-effects)○ (hint: its not)● OpCodes are really Pointers○ Swizzling!!!
    • In Other Words
    • OpCode CachingIs Hard!
    • Time To Execute!
    • Zend/zend_vm_execute.h
    • PHPPHP/Engine/Executor.php
    • Executor PipelineOpCodeIsReturn?No YesReturn
    • But What Are WeExecuting?
    • Zend/zend_vm_execute.h
    • Interesting Note:vm_execute.hIs Generated ByPHP
    • 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" WhenRefCount = 0● Enables Primitive GarbageCollection○ Circular GC is also implemented
    • Thats All There IsTo It!
    • Lets Look At AnExample
    • $a = 1;$b = 2;var_dump($a + $b);
    • line # op return operands-----------------------------------2 0 ASSIGN !0, 13 1 ASSIGN !1, 26 2 ADD ~2 !0, !13 SEND_VAL ~24 DO_FCALL var_dump5 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
    • Theres A TonMore
    • Get Involved!
    • More Info●●●● Reference Series○○■ PHP Internals Series
    • Anthony