INTRO<br /><ul><li>Senior Frontend Engineer @ Tuenti
C#/.NET background, now a bit of PHP knowledge
diego@tuenti.com
twitter.com/kartones</li></li></ul><li>AGENDA<br /><ul><li>The Goal
Shared Hosting to Tuenti-like scale webs
Typical PHP
PHP Practices
Coding Practices
Web Practices</li></li></ul><li>THE GOAL<br />
THE REAL GOAL<br />Avoid the PHP joke<br />
THE REAL REAL GOAL<br />Build quality PHP code<br />
SHARED HOSTING<br />Internet<br />Frontend + DB<br />
FIRST SPLIT<br />Internet<br />Frontend<br />DB<br />
MORE FRONTENDS<br />Internet<br />Frontends<br />DB<br />
CACHING TIME<br />Internet<br />Frontends<br />Cache<br />DB<br />
MORE CACHING<br />Internet<br />Frontends<br />Cache<br />DB<br />
MASTER/SLAVE DBS<br />Internet<br />Frontends<br />Cache<br />DBs<br />
TUENTI (OVERVIEW)<br />Internet<br />Farm 1<br />Farm 2<br />Farm N<br />Others<br />Frontends<br />Caches<br />DBs<br />
TYPICAL PHP<br /><ul><li>HTML + PHP script blocks + DB Queries
If lucky, separated into ¨functions¨ and templates (PHPBB, Wordpress…)
No Object Orientation</li></li></ul><li>TYPICAL PHP<br />News since your last visit:<br /><ul><br /><? <br />$e = $_POST['...
TYPICAL PHP<br />News since your last visit:<br /><ul><br /><? <br />$e = $_POST['email'];<br />$news = mysql_query("SELEC...
TYPICAL PHP<br />News since your last visit:<br /><ul><br /><? <br />$e = $_POST['email'];<br />$news = mysql_query("SELEC...
TYPICAL PHP<br />News since your last visit:<br /><ul><br /><? <br />$e = $_POST['email'];<br />$news = mysql_query("SELEC...
TYPICAL PHP<br />function crop_string($string) {<br /> if (strlen($string) > 30) {<br />  $string = substr($string, 0, 30)...
TYPICAL PHP<br />class StringHelper<br />{<br />const CROP_ELLIPSIS = ‘…’;const CROP_DEFAULT_SIZE = 30;<br /> public stati...
PHP PRACTICES<br /><ul><li>PHP 5.3 (or the newest stable version)
Object Orientation
Namespaces / structured source code tree</li></li></ul><li>PHP PRACTICES<br /><ul><li>Layered code
MVC is typical and good</li></ul>Controller<br />Model<br />View<br />
PHP PRACTICES<br /><ul><li>Breaking loops is ugly</li></ul>for($i = 0; $i < count($items); $i++)<br />{<br />	if ($items[$...
PHP PRACTICES<br /><ul><li>Break-free</li></ul>$found = false;<br />for($i = 0; $i < count($items) && !$found; $i++)<br />...
PHP PRACTICES<br /><ul><li> Try to keep memory usage low
Less memory, more concurrent PHP processes
 unset()
 ini_set(“memory_limit”,”8M”);</li></li></ul><li>PHP PRACTICES<br /><ul><li>Singleton in PHP != Singleton in Java/C#/C++
Upcoming SlideShare
Loading in …5
×

Hardcore PHP

3,962 views

Published on

Diego "Kartones" Muñoz mostrará ciertos trucos y metodología de PHP y AJAX para desarrollos del calibre de Tuenti, empresa que todos conocemos.

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,962
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Hardcore PHP

  1. 1.
  2. 2.
  3. 3. INTRO<br /><ul><li>Senior Frontend Engineer @ Tuenti
  4. 4. C#/.NET background, now a bit of PHP knowledge
  5. 5. diego@tuenti.com
  6. 6. twitter.com/kartones</li></li></ul><li>AGENDA<br /><ul><li>The Goal
  7. 7. Shared Hosting to Tuenti-like scale webs
  8. 8. Typical PHP
  9. 9. PHP Practices
  10. 10. Coding Practices
  11. 11. Web Practices</li></li></ul><li>THE GOAL<br />
  12. 12. THE REAL GOAL<br />Avoid the PHP joke<br />
  13. 13. THE REAL REAL GOAL<br />Build quality PHP code<br />
  14. 14. SHARED HOSTING<br />Internet<br />Frontend + DB<br />
  15. 15. FIRST SPLIT<br />Internet<br />Frontend<br />DB<br />
  16. 16. MORE FRONTENDS<br />Internet<br />Frontends<br />DB<br />
  17. 17. CACHING TIME<br />Internet<br />Frontends<br />Cache<br />DB<br />
  18. 18. MORE CACHING<br />Internet<br />Frontends<br />Cache<br />DB<br />
  19. 19. MASTER/SLAVE DBS<br />Internet<br />Frontends<br />Cache<br />DBs<br />
  20. 20. TUENTI (OVERVIEW)<br />Internet<br />Farm 1<br />Farm 2<br />Farm N<br />Others<br />Frontends<br />Caches<br />DBs<br />
  21. 21. TYPICAL PHP<br /><ul><li>HTML + PHP script blocks + DB Queries
  22. 22. If lucky, separated into ¨functions¨ and templates (PHPBB, Wordpress…)
  23. 23. No Object Orientation</li></li></ul><li>TYPICAL PHP<br />News since your last visit:<br /><ul><br /><? <br />$e = $_POST['email'];<br />$news = mysql_query("SELECT * FROM news WHERE email='{$e}'");[…]<br />foreach($news as $newsItem)<br />{<br /> ?><br /> <li><?=$newsItem[0]?></li><br /><?<br />}<br />?><br /></ul><br />
  24. 24. TYPICAL PHP<br />News since your last visit:<br /><ul><br /><? <br />$e = $_POST['email'];<br />$news = mysql_query("SELECT * FROM news WHERE email='{$e}'");<br />[…]<br />foreach($news as $newsItem)<br />{<br /> ?><br /> <li><?=$newsItem[0]?></li><br /><?<br />}<br />?><br /></ul><br />
  25. 25. TYPICAL PHP<br />News since your last visit:<br /><ul><br /><? <br />$e = $_POST['email'];<br />$news = mysql_query("SELECT * FROM news WHERE email='{$e}'");<br />[…]<br />foreach($news as $newsItem)<br />{<br /> ?><br /> <li><?=$newsItem[0]?></li><br /><?<br />}<br />?><br /></ul><br />
  26. 26. TYPICAL PHP<br />News since your last visit:<br /><ul><br /><? <br />$e = $_POST['email'];<br />$news = mysql_query("SELECT * FROM news WHERE email='{$e}'");<br />[…]<br />foreach($news as $newsItem)<br />{<br /> ?><br /> <li><?=$newsItem[0]?></li><br /><?<br />}<br />?><br /></ul><br />
  27. 27. TYPICAL PHP<br />function crop_string($string) {<br /> if (strlen($string) > 30) {<br /> $string = substr($string, 0, 30) . “…”;<br /> }<br />return $string;<br />}<br />$text = crop_string(“developers,developers,developers,developers”);<br />
  28. 28. TYPICAL PHP<br />class StringHelper<br />{<br />const CROP_ELLIPSIS = ‘…’;const CROP_DEFAULT_SIZE = 30;<br /> public static function Crop($text, <br /> $cropLength = self::CROP_DEFAULT_SIZE) {<br /> if (mb_strlen($text) > $cropLength) {<br /> $croppedText = substr($text,0,$cropLength) . Self::CROP_ELLIPSIS;<br /> } else {<br />$croppedText = $text;<br /> }<br /> return $croppedText;<br />}<br />$text = StringHelper::Crop(“developers,developers,developers,developers”);<br />
  29. 29. PHP PRACTICES<br /><ul><li>PHP 5.3 (or the newest stable version)
  30. 30. Object Orientation
  31. 31. Namespaces / structured source code tree</li></li></ul><li>PHP PRACTICES<br /><ul><li>Layered code
  32. 32. MVC is typical and good</li></ul>Controller<br />Model<br />View<br />
  33. 33. PHP PRACTICES<br /><ul><li>Breaking loops is ugly</li></ul>for($i = 0; $i < count($items); $i++)<br />{<br /> if ($items[$i] == searchedItem)<br />{<br />break;<br /> }<br />}<br />
  34. 34. PHP PRACTICES<br /><ul><li>Break-free</li></ul>$found = false;<br />for($i = 0; $i < count($items) && !$found; $i++)<br />{<br /> if ($items[$i] == searchedItem)<br /> {<br />$found = true;<br /> }<br />}<br />
  35. 35. PHP PRACTICES<br /><ul><li> Try to keep memory usage low
  36. 36. Less memory, more concurrent PHP processes
  37. 37. unset()
  38. 38. ini_set(“memory_limit”,”8M”);</li></li></ul><li>PHP PRACTICES<br /><ul><li>Singleton in PHP != Singleton in Java/C#/C++
  39. 39. Same PHP execution = same singleton
  40. 40. 2 page requests = 2 different singletons
  41. 41. Terribly dangerous in tests
  42. 42. Implement a ¨flushSingleton()¨ static method</li></li></ul><li>PHP PRACTICES<br /><ul><li>Homogeneous code
  43. 43. Comments
  44. 44. @author tag (Sign your code!)
  45. 45. Proper variables casing & naming
  46. 46. Good source tree = easy to guess where to find a class
  47. 47. Avoids personal bad practices</li></li></ul><li>PHP PRACTICES<br /><ul><li>Avoid non testeable objects</li></ul>class Game {<br /> private $player1 = new GamePlayer();<br />private $player2 = new GamePlayer();<br />public function Play() {<br />// Logic that uses $player1 & $player2<br /> }<br />}<br />
  48. 48. PHP PRACTICES<br /><ul><li>Create testeable objects</li></ul>class Game {<br /> private $player1 = null;<br />private $player2 = null;<br /> public function __construct(IGameEntity $playerA, <br /> IGameEntity $playerB) {<br /> $this->player1 = $playerA;<br />$this->player2 = $playerB;<br /> }<br />public function Play() {<br />// Logic that uses $player1 & $player2<br /> }<br />}<br />
  49. 49. PHP PRACTICES<br /><ul><li>Defensive Programming
  50. 50. defined()
  51. 51. isset()
  52. 52. class_exists()
  53. 53. method_exists()</li></li></ul><li>CODING PRACTICES<br />Learn & use Source Code Control<br /><ul><li>Distributed
  54. 54. Best options: SVN,Git, Mercurial
  55. 55. Always linked to a ticket control system
  56. 56. Learn to branch, diff, merge, resolve conflicts
  57. 57. Hard at first, pays off in big projects</li></li></ul><li>CODING PRACTICES<br />Learn & do Testing<br /><ul><li>Unit tests
  58. 58. Test DB data (Fixtures)
  59. 59. Mock Objects
  60. 60. Integration tests
  61. 61. Acceptance tests</li></li></ul><li>WEB PRACTICES<br /><ul><li>Learn Kung-fu:
  62. 62. HTTP protocol basics
  63. 63. Some Javascript
  64. 64. Minimal CSS
  65. 65. Robots.txt
  66. 66. Favicon.ico
  67. 67. Sitemap.xml
  68. 68. Cookies
  69. 69. Encoding
  70. 70. Web Security basics</li></li></ul><li>WEB PRACTICES<br /><ul><li>Minimize HTML, CSS, JS
  71. 71. Google closure Compiler
  72. 72. YuiCompressor
  73. 73. Firebug
  74. 74. Use tools to detect improvements:
  75. 75. PageSpeed (Firefox/Chrome)
  76. 76. YSlow (Firefox)
  77. 77. MySpace Performance Tracker (IE)</li></li></ul><li>WEB PRACTICES<br /><ul><li>Use the client to store data
  78. 78. Cookies (4KB max)
  79. 79. LocalStorage (HTML5)
  80. 80. Global scoped Javascript variables (AJAX only)
  81. 81. Javascript Datasources (Tuenti AJAX)</li></li></ul><li>WEB PRACTICES<br /><ul><li>If you don’t need realtime, be lazy
  82. 82. Lazy loading
  83. 83. Lazy deletion
  84. 84. Job queues instead of realtime operations</li></li></ul><li>THE END<br />¿Questions?<br />http://dev.tuenti.com<br />http://jobs.tuenti.com<br />

×