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

PHP, Under The Hood - DPC

on

  • 16,017 views

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!

Statistics

Views

Total Views
16,017
Views on SlideShare
14,094
Embed Views
1,923

Actions

Likes
4
Downloads
83
Comments
1

11 Embeds 1,923

http://blog.ircmaxell.com 1849
https://twitter.com 26
http://eventifier.co 25
http://librosweb.es 7
http://www.rickardranniger.se 6
http://www.eventifier.co 4
http://eventifier.com 2
http://plus.url.google.com 1
http://roojs.com 1
http://www.librosweb.es 1
http://7414445313853167451_8211ed460cb8898cc0459832f6d341d32dc18989.blogspot.com 1
More...

Accessibility

Categories

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.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • post more
    Are you sure you want to
    Your message goes here
    Processing…
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● github.com/php/php-src● lxr.php.net● github.com/ircmaxell/PHPPHP● Reference Series○ wiki.php.net○ blog.ircmaxell.com■ PHP Internals Series
    • Anthony FerraraJoind.in/8443@ircmaxellblog.ircmaxell.comircmaxell@php.netyoutube.com/ircmaxell