Successfully reported this slideshow.

PHP, Under The Hood - DPC

21,258 views

Published on

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!

Published in: Technology
  • post more
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

PHP, Under The Hood - DPC

  1. 1. PHPUnder The Hood
  2. 2. What Is PHP?● An Easy To Learn Language?○ Sure!● A Simple Language?○ Usually● A Well Designed Language?○ HA!● An Effective Language!○ Absolutely!
  3. 3. PHP Is...DynamicallyTyped
  4. 4. $a = 1;$b = "a";
  5. 5. PHP Is...Weak Typed
  6. 6. 1 + "1b" == 2
  7. 7. PHP Is...ImplementationDefined
  8. 8. ImplementationsMain:C-PHP: www.php.netAlt:HipHop - FacebookRoadsend - CompilerPhalanger - .NETPHP Compiler - CompilerPHPPHP - PHP
  9. 9. Interesting Note:C-PHP RequiresPHP To Compile!
  10. 10. PHP Is...Compiled
  11. 11. PHP Is...DynamicallyCompiledOn-Demand
  12. 12. It All Starts With TheSAPI
  13. 13. SAPI● Server API● "Starts" PHP and issues request(s)● Common SAPIs:○ CLI○ CGI (and FastCGI)○ mod_php○ FPM○ etc...
  14. 14. Execution Pipeline
  15. 15. Execution PipelineStart UpRequestShut DownSAPI
  16. 16. Execution PipelineStart UpRequestShut DownSAPIConfig InitEngine InitModule Init
  17. 17. Zend/zend.c
  18. 18. PHPPHP/PHP.php
  19. 19. Execution PipelineStart UpRequestShut DownSAPIModuleShutdownEngineShutdown
  20. 20. Zend/zend.c
  21. 21. Execution PipelineStart UpRequestShut DownSAPIRequest InitCompile CodeExecute CodeRequestShutdown
  22. 22. Compiler PipelineLexer
  23. 23. Lexer Step● C-PHP○ Tokenizes using RE2C○ Produces Array of Tokens● PHPPHP○ Uses core tokenizer (or emulative)○ PHPParser Powered
  24. 24. Zend/zend_language_scanner.l
  25. 25. PHPParser/Lexer/Emulative.php
  26. 26. Compiler PipelineLexerParser
  27. 27. Parse Step● C-PHP○ Directly Generates OPCode Array○ BISON Powered● PHPPHP○ Generates AST Structure○ PHPParser Powered
  28. 28. Zend/zend_language_parser.y
  29. 29. Compiler PipelineLexerParserCompiler
  30. 30. Compile Step● C-PHP○ Compiled Directly During Parse○ Single Pass Compilation● PHPPHP○ Recurses Over AST○ Single Pass (for now)○
  31. 31. PHPPHP/Engine/Compiler.php
  32. 32. Compiler PipelineLexerParserCompilerOpCode
  33. 33. OpCodes● A Series of Operations○ Just like Assembler Opcodes○ Represent "units of functionality"● Designed to run on Virtual Machine○ Zend Engine○ Or PHPPHP!
  34. 34. $a = 1;$b = 2;echo $a + $b;
  35. 35. Notice Anything?
  36. 36. What If WeCached TheOpCodes?
  37. 37. We Can Cache!● Given the compiler is Idempodent○ (has no side-effects)○ (hint: its not)● OpCodes are really Pointers○ Swizzling!!!
  38. 38. In Other Words
  39. 39. OpCode CachingIs Hard!
  40. 40. Time To Execute!
  41. 41. Zend/zend_vm_execute.h
  42. 42. PHPPHP/Engine/Executor.php
  43. 43. Executor PipelineOpCodeIsReturn?No YesReturn
  44. 44. But What Are WeExecuting?
  45. 45. Zend/zend_vm_execute.h
  46. 46. Interesting Note:vm_execute.hIs Generated ByPHP
  47. 47. PHPPHP/Engine/OpLines/Add.php
  48. 48. Variables!
  49. 49. Zend/zend.h
  50. 50. PHPPHP/Engine/Zval/Value.php
  51. 51. Ref-Counting● RefCount + References○ Allows Copy-On-Write● Variable Is "Deleted" WhenRefCount = 0● Enables Primitive GarbageCollection○ Circular GC is also implemented
  52. 52. Thats All There IsTo It!
  53. 53. Lets Look At AnExample
  54. 54. $a = 1;$b = 2;var_dump($a + $b);
  55. 55. 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
  56. 56. [0] => PHPPHPEngineOpLinesAssign[1] => PHPPHPEngineOpLinesAssign[2] => PHPPHPEngineOpLinesAdd[3] => PHPPHPEngineOpLinesInitFCallByName[4] => PHPPHPEngineOpLinesSend[5] => PHPPHPEngineOpLinesFunctionCall[6] => PHPPHPEngineOpLinesReturnOp
  57. 57. [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)
  58. 58. PHPPHP/Engine/OpLines/Assign.php
  59. 59. PHPPHP/Engine/OpLines/Add.php
  60. 60. PHPPHP/Engine/OpLines/InitFCallByName.php
  61. 61. PHPPHP/Engine/OpLines/Send.php
  62. 62. PHPPHP/Engine/OpLines/FunctionCall.php
  63. 63. Theres A TonMore
  64. 64. Get Involved!
  65. 65. 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
  66. 66. Anthony FerraraJoind.in/8443@ircmaxellblog.ircmaxell.comircmaxell@php.netyoutube.com/ircmaxell

×