TAKING PHP
SERIOUSLY
Keith Adams
Facebook
Strange Loop 2013
Why PHP?
What this talk is
•  Experience report
•  Apologia
•  Qualified advocacy
•  A surprise.
What this talk is not
•  “Network effects”/”Legacy”
•  “Right tool for the job”
•  tautological
•  some tools really are b...
Recent changes
•  Traits (ala Scala)
•  Closures
•  Generators (yield statement)
•  The HipHop VM (hhvm) is fast
•  https:...
Conventional Wisdom on PHP
•  “PHP: A fractal of bad design”
•  http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-...
And yet...
•  A lot of software that has changed the world has been
rendered in PHP
•  Mediawiki
•  Facebook
•  Wordpress
...
Facebook’s PHP Codebase
•  x * 105 files
•  y * 107 LoC
•  10 releases per week
•  Anecdotally, good engineers are astonis...
The Case Against PHP
•  Unexpected behaviors
$x	
  /	
  0	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  =>	
  bool(fals...
The Case Against PHP (2)
•  Schizophrenia about value/reference semantics
	
  	
  /*	
  	
  
	
  	
  	
  *	
  Probably	
  ...
The Case Against PHP (3)
•  Reliance on reference-counting
•  String, array need O(1) logical copies
•  User-defined class...
The Case Against PHP (4)
•  Inconsistent, dangerous standard library
•  array_map vs. array_reduce argument orders
•  arra...
The Case Against PHP: “Guilty”
•  It’s all true!
•  These are “unforced errors”
•  Most other languages do better
•  You w...
In Defense of PHP
•  PHP gets three important things really right
•  Programmer workflow
•  State
•  Concurrency
Workflow
•  Save, reload-the-page
•  Short feedback cycle
•  Optimizes most precious resource of all: programmer
short-ter...
State
•  PHP requests always start with empty heap, namespace
•  Cross-request state must be saved explicitly
•  Filesyste...
Concurrency
•  PHP requests execute in a single thread
•  Concurrency happens via recursive web requests
•  shared-nothing...
Workflow
StateConcurrency
The limits of conscious design
•  Discovered or invented?
•  Shrug
•  In my opinion, more important than PHP’s problems
• ...
Pushing PHP further
•  PHP engineer dare: rename this method
•  Reorder the parameters for this method
•  Remove this meth...
Pushing PHP further (2)
•  Enforce invariants:
•  Only escaped strings are passed to build_query	
  
•  A certain array() ...
Wait...
•  A static type system?
•  Verbose types, or
incomprehensible error
messages
•  Either way hoses
programmer produ...
We think we’ve solved this problem
•  Introducing Hack
•  Gradual typing for PHP
•  Novel type inference system
•  Real-ti...
Hack
•  Opt into typing via <?hh (instead of <?php)
•  <?hh	
  //	
  strict	
  
•  Almost-totally sound analysis
•  Requir...
Hack implementation
EditorType Checker HHVM
Filesystem
inotify
errors
web
requests
source
Changes from PHP
<?hh	
  
class	
  Point2	
  {	
  
	
  	
  public	
  float	
  $x,	
  $y;	
  
	
  	
  function	
  __constru...
Changes from PHP
<?hh	
  
class	
  Point2	
  {	
  
	
  	
  public	
  float	
  $x,	
  $y;	
  
	
  	
  function	
  __constru...
Changes from PHP
<?hh	
  
...	
  
function	
  meanOrigDistance(Point	
  $p,	
  Point	
  $q)	
  
	
  	
  :	
  float	
  {	
 ...
Hack Type Cheatsheet
•  Base PHP types: int,	
  MyClassName,	
  array,	
  ...	
  
•  Nullable: ?int,	
  ?MyClassName	
  
•...
Hack Type Inference (1)
•  Let’s infer the type of $x:
Hack Type Inference (2)
•  How does a type-system normally work?
•  Type-variables are introduced
•  A unification algorit...
Type inference in Hack
•  Hack introduces unresolved types (noted U)
if (…) {
$x = new A();
} else {
$x = new B();
}
takes...
Error messages
•  We can’t expect the user to understand all the type-
inference
•  The solution: keep the reason why we d...
Hack
•  “[ X ] You have reinvented PHP better, but that’s still no
justification
•  [ X ] The name of your language makes ...
Postmodern PHP (2014-...)
•  HipHop project provides
great tools
•  Fast VM
•  Debugger
•  Profiler
•  Integrations with e...
When PHP?
•  Any time you might consider another “Dynamic Algol”
language
•  Python, Lua, JavaScript, Perl, Ruby, ...
•  S...
Backup
Everyone’s favorite generics slide
•  (Remember, “covariance” refers to type specifications for Type that accept T
>= Type...
TAKING PHP SERIOUSLY - Keith Adams
Upcoming SlideShare
Loading in...5
×

TAKING PHP SERIOUSLY - Keith Adams

15,058

Published on

TAKING PHP SERIOUSLY
Keith Adams Facebook
Strange Loop 2013

Published in: Technology
5 Comments
7 Likes
Statistics
Notes
  • http://gg.gg/3osrb
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Free Download : http://gg.gg/114bb

    Hey Guyz and girls, Today I am gonna show you perfact tool, Remember This video is old, But the download link with hack is brand new. Its very simple to using this tool and here are some instructions in video. Please REDOWNLOAD. Don't Forget to Comment Subscribe & Rate My Video :)

    Virus Scan :- This file has been scanned with avast! Antivirus. -- Status: FILE IS CLEANN.

    Copyright © 2014. All Rights Reserved
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Free Download : http://gg.gg/114bb

    Hey Guyz and girls, Today I am gonna show you perfact tool, Remember This video is old, But the download link with hack is brand new. Its very simple to using this tool and here are some instructions in video. Please REDOWNLOAD. Don't Forget to Comment Subscribe & Rate My Video :)

    Virus Scan :- This file has been scanned with avast! Antivirus. -- Status: FILE IS CLEANN.

    Copyright © 2014. All Rights Reserved
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hi! Where I can find more info about 'hack php', this is amazing!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
15,058
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
5
Likes
7
Embeds 0
No embeds

No notes for slide

Transcript of "TAKING PHP SERIOUSLY - Keith Adams"

  1. 1. TAKING PHP SERIOUSLY Keith Adams Facebook Strange Loop 2013
  2. 2. Why PHP?
  3. 3. What this talk is •  Experience report •  Apologia •  Qualified advocacy •  A surprise.
  4. 4. What this talk is not •  “Network effects”/”Legacy” •  “Right tool for the job” •  tautological •  some tools really are bad •  PHP might be such a tool •  “Worse is better” •  pace Richard Gabriel •  Better is better •  Most people think of UNIX as “better” nowadays
  5. 5. Recent changes •  Traits (ala Scala) •  Closures •  Generators (yield statement) •  The HipHop VM (hhvm) is fast •  https://github.com/facebook/hiphop-php/ •  https://www.hhvm.com •  ...and we want it to run your code •  http://www.hhvm.com/blog/?p=875
  6. 6. Conventional Wisdom on PHP •  “PHP: A fractal of bad design” •  http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/ •  “[ ] You have reinvented PHP better, but that’s still no justification” •  http://colinm.org/language_checklist.html •  Etc.
  7. 7. And yet... •  A lot of software that has changed the world has been rendered in PHP •  Mediawiki •  Facebook •  Wordpress •  Drupal •  This is at least interesting •  Should they really have been written in Haskell? •  Does PHP make projects more or less successful?
  8. 8. Facebook’s PHP Codebase •  x * 105 files •  y * 107 LoC •  10 releases per week •  Anecdotally, good engineers are astonishingly productive in PHP
  9. 9. The Case Against PHP •  Unexpected behaviors $x  /  0                      //  =>  bool(false)     “11abcd”  +  “1xy”  //  =>  int(12)   “0123”  +  “3456”    //  =>  int(3579)   “0123”  |  “3456”    //  =>  string(“3577”)  
  10. 10. The Case Against PHP (2) •  Schizophrenia about value/reference semantics    /*          *  Probably  copy  $a  into  foo’s  0’th  param.        *  Unless  $a  is  a  user-­‐defined  object;  and  unless        *  foo’s  definition  specifies  that  arg  0  is  by          *  reference.        */      foo($a);  
  11. 11. The Case Against PHP (3) •  Reliance on reference-counting •  String, array need O(1) logical copies •  User-defined classes have destructors that run at a deterministic time •  Some programs use the RAII idiom from C++ •  Heavily constrains implementation
  12. 12. The Case Against PHP (4) •  Inconsistent, dangerous standard library •  array_map vs. array_reduce argument orders •  array_merge •  mysql_escape_string vs. (sigh) mysql_real_escape_string
  13. 13. The Case Against PHP: “Guilty” •  It’s all true! •  These are “unforced errors” •  Most other languages do better •  You would want to avoid them in a PHP Reboot
  14. 14. In Defense of PHP •  PHP gets three important things really right •  Programmer workflow •  State •  Concurrency
  15. 15. Workflow •  Save, reload-the-page •  Short feedback cycle •  Optimizes most precious resource of all: programmer short-term memory
  16. 16. State •  PHP requests always start with empty heap, namespace •  Cross-request state must be saved explicitly •  Filesystem, memcache, APC •  Affirmative virtue •  Typical FB requests spend 10ms initializing •  Reduces the cost of bugs •  Requests interact in limited ways •  Natural boundary for failure isolation
  17. 17. Concurrency •  PHP requests execute in a single thread •  Concurrency happens via recursive web requests •  shared-nothing •  inputs/outputs copied •  Limits PHP’s applicable domain •  That’s actually good.
  18. 18. Workflow StateConcurrency
  19. 19. The limits of conscious design •  Discovered or invented? •  Shrug •  In my opinion, more important than PHP’s problems •  They’re not available anywhere else Workflow StateConcurrency
  20. 20. Pushing PHP further •  PHP engineer dare: rename this method •  Reorder the parameters for this method •  Remove this method that we think is not called anywhere
  21. 21. Pushing PHP further (2) •  Enforce invariants: •  Only escaped strings are passed to build_query   •  A certain array() maps strings to Widgets
  22. 22. Wait... •  A static type system? •  Verbose types, or incomprehensible error messages •  Either way hoses programmer productivity •  Millions of lines to migrate Workflow StateConcurrency
  23. 23. We think we’ve solved this problem •  Introducing Hack •  Gradual typing for PHP •  Novel type inference system •  Real-time type-checking preserves PHP workflow •  Credit: Julien Verlaguet
  24. 24. Hack •  Opt into typing via <?hh (instead of <?php) •  <?hh  //  strict   •  Almost-totally sound analysis •  Requires transitive closure of code has been hackified   •  <?hh   •  Tolerates missing annotations •  Assumes undeclared classes/functions exist, behave as implied by any types •  Disallows most “silly” PHP-isms
  25. 25. Hack implementation EditorType Checker HHVM Filesystem inotify errors web requests source
  26. 26. Changes from PHP <?hh   class  Point2  {      public  float  $x,  $y;      function  __construct(float  $x,  float  $y)  {          $this-­‐>x  =  $x;          $this-­‐>x  =  $y;      }   }  
  27. 27. Changes from PHP <?hh   class  Point2  {      public  float  $x,  $y;      function  __construct(float  $x,  float  $y)  {          $this-­‐>x  =  $x;          $this-­‐>x  =  $y;  //  Whoopsy.  Didn’t  init  y      }   }  
  28. 28. Changes from PHP <?hh   ...   function  meanOrigDistance(Point  $p,  Point  $q)      :  float  {      $distf  =  function(Point  $p)  :  float  {          return  sqrt($p-­‐>x  *  $p-­‐>x  +  $p-­‐>y  *  $p-­‐>y);      };      $pdist  =  $distf($p);      $qdist  =  $distf($q);      return  ($pdist  +  $qdist)  /  2;   }  
  29. 29. Hack Type Cheatsheet •  Base PHP types: int,  MyClassName,  array,  ...   •  Nullable: ?int,  ?MyClassName   •  Mixed: anything (careful) •  Tuples: (int,  bool,  X)   •  Closures: (function(int):  int)   •  Collections: Vector<int>,  Map<string,  int>   •  Generics: A<T>,  foo<T>(T  $x):  T   •  Constraints: foo<T  as  A>(T  $x):  T  
  30. 30. Hack Type Inference (1) •  Let’s infer the type of $x:
  31. 31. Hack Type Inference (2) •  How does a type-system normally work? •  Type-variables are introduced •  A unification algorithm solves the type-variables (usually noted α) if (…) { $x = new A(); } else { $x = new B(); } type($x) = α unify(α, A) => α = A unify(α, B) => α = B ERROR
  32. 32. Type inference in Hack •  Hack introduces unresolved types (noted U) if (…) { $x = new A(); } else { $x = new B(); } takesAnIFace($x); type($x) = α = U() $x = α = U(A); $x = α = U(A, B); $x = α = U(A, B) = IFace with (A ≤ IFace, B ≤ IFace)
  33. 33. Error messages •  We can’t expect the user to understand all the type- inference •  The solution: keep the reason why we deduced a type and expose it to the user
  34. 34. Hack •  “[ X ] You have reinvented PHP better, but that’s still no justification •  [ X ] The name of your language makes it impossible to find on Google” •  Many millions of lines converted •  Most new code in Hack •  Most PHP users at Facebook regularly check in Hack
  35. 35. Postmodern PHP (2014-...) •  HipHop project provides great tools •  Fast VM •  Debugger •  Profiler •  Integrations with editors/IDEs •  Hack is a SoA gradual typing system •  Maintains all of PHP’s strengths •  Compare to your favorite “Dynamic Algol” Workflow StateConcurrency
  36. 36. When PHP? •  Any time you might consider another “Dynamic Algol” language •  Python, Lua, JavaScript, Perl, Ruby, ... •  Server-side •  Request-oriented •  ...but want to preserve some of the option value of “BigLangs” •  Type system •  High-performance implementations
  37. 37. Backup
  38. 38. Everyone’s favorite generics slide •  (Remember, “covariance” refers to type specifications for Type that accept T >= Type. “Contravariance” means Type that accept T <= Type.) •  We allow: •  Covariant function parameters •  Covariant arrays •  Constraints on type parameters (Foo<T as IFace> will error if T does not implement IFace) •  We don’t allow •  Contravariant function params (they don’t make sense) •  Covariant type parameters •  Remember, runtime throws everything away anyway, so perfwise, it’s type erasure.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×