hardcore php

38

Upload: roberto-luis-bisbe

Post on 11-May-2015

3.456 views

Category:

Education


4 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Hardcore PHP
Page 2: Hardcore PHP
Page 3: Hardcore PHP

INTRO

• Senior Frontend Engineer @ Tuenti• C#/.NET background, now a bit of PHP

knowledge• [email protected]• twitter.com/kartones

Page 4: Hardcore PHP

AGENDA

• The Goal• Shared Hosting to Tuenti-like scale

webs• Typical PHP• PHP Practices• Coding Practices• Web Practices

Page 5: Hardcore PHP

THE GOAL

Page 6: Hardcore PHP

THE REAL GOAL

Avoid the PHP joke

Page 7: Hardcore PHP

THE REAL REAL GOAL

Build quality PHP code

Page 8: Hardcore PHP

SHARED HOSTING

Internet

Frontend + DB

Page 9: Hardcore PHP

FIRST SPLIT

Internet

Frontend

DB

Page 10: Hardcore PHP

MORE FRONTENDS

Internet

Frontends

DB

Page 11: Hardcore PHP

CACHING TIME

Internet

Frontends

DB

Cache

Page 12: Hardcore PHP

MORE CACHING

Internet

Frontends

DB

Cache

Page 13: Hardcore PHP

MASTER/SLAVE DBS

Internet

Frontends

DBs

Cache

Page 14: Hardcore PHP

TUENTI (OVERVIEW)

Internet

Farm 1 Farm 2 Farm N Others

Frontends

Caches

DBs

Page 15: Hardcore PHP

TYPICAL PHP

•HTML + PHP script blocks + DB Queries•If lucky, separated into ¨functions¨ and templates (PHPBB, Wordpress…)•No Object Orientation

Page 16: Hardcore PHP

TYPICAL PHPNews since your last visit:<ul><? $e = $_POST['email'];$news = mysql_query("SELECT * FROM news WHERE email='{$e}'");[…]foreach($news as $newsItem){ ?> <li><?=$newsItem[0]?></li> <?}?></ul>

Page 17: Hardcore PHP

TYPICAL PHPNews since your last visit:<ul><? $e = $_POST['email'];$news = mysql_query("SELECT * FROM news WHERE email='{$e}'");[…]foreach($news as $newsItem){ ?> <li><?=$newsItem[0]?></li> <?}?></ul>

Page 18: Hardcore PHP

TYPICAL PHPNews since your last visit:<ul><? $e = $_POST['email'];$news = mysql_query("SELECT * FROM news WHERE email='{$e}'");[…]foreach($news as $newsItem){ ?> <li><?=$newsItem[0]?></li> <?}?></ul>

Page 19: Hardcore PHP

TYPICAL PHPNews since your last visit:<ul><? $e = $_POST['email'];$news = mysql_query("SELECT * FROM news WHERE email='{$e}'");[…]foreach($news as $newsItem){ ?> <li><?=$newsItem[0]?></li> <?}?></ul>

Page 20: Hardcore PHP

TYPICAL PHPfunction crop_string($string) { if (strlen($string) > 30) { $string = substr($string, 0, 30) . “…”; } return $string;}

$text = crop_string(“developers,developers,developers,developers”);

Page 21: Hardcore PHP

TYPICAL PHPclass StringHelper{ const CROP_ELLIPSIS = ‘…’; const CROP_DEFAULT_SIZE = 30;

public static function Crop($text, $cropLength = self::CROP_DEFAULT_SIZE) { if (mb_strlen($text) > $cropLength) { $croppedText = substr($text,0,$cropLength) . Self::CROP_ELLIPSIS; } else { $croppedText = $text; } return $croppedText;}

$text = StringHelper::Crop(“developers,developers,developers,developers”);

Page 22: Hardcore PHP

PHP PRACTICES

•PHP 5.3 (or the newest stable version)•Object Orientation•Namespaces / structured source code tree

Page 23: Hardcore PHP

PHP PRACTICES

•Layered code•MVC is typical and good

Controller

ModelView

Page 24: Hardcore PHP

PHP PRACTICES

•Breaking loops is uglyfor($i = 0; $i < count($items); $i++){

if ($items[$i] == searchedItem){

break;}

}

Page 25: Hardcore PHP

PHP PRACTICES

•Break-free$found = false;for($i = 0; $i < count($items) && !$found; $i++){

if ($items[$i] == searchedItem){

$found = true;}

}

Page 26: Hardcore PHP

PHP PRACTICES

• Try to keep memory usage low• Less memory, more concurrent PHP processes• unset()• ini_set(“memory_limit”,”8M”);

Page 27: Hardcore PHP

PHP PRACTICES

•Singleton in PHP != Singleton in Java/C#/C++•Same PHP execution = same singleton•2 page requests = 2 different singletons•Terribly dangerous in tests• Implement a ¨flushSingleton()¨ static method

Page 28: Hardcore PHP

PHP PRACTICES

•Homogeneous code•Comments•@author tag (Sign your code!)•Proper variables casing & naming•Good source tree = easy to guess where to find a class•Avoids personal bad practices

Page 29: Hardcore PHP

PHP PRACTICES

• Avoid non testeable objectsclass Game { private $player1 = new GamePlayer(); private $player2 = new GamePlayer(); public function Play() { // Logic that uses $player1 & $player2 }}

Page 30: Hardcore PHP

PHP PRACTICES

• Create testeable objectsclass Game { private $player1 = null; private $player2 = null; public function __construct(IGameEntity $playerA, IGameEntity $playerB) { $this->player1 = $playerA; $this->player2 = $playerB; }

public function Play() { // Logic that uses $player1 & $player2 }}

Page 31: Hardcore PHP

PHP PRACTICES

• Defensive Programming• defined()• isset()• class_exists()• method_exists()

Page 32: Hardcore PHP

CODING PRACTICES

Learn & use Source Code Control•Distributed• Best options: SVN,Git, Mercurial

• Always linked to a ticket control system• Learn to branch, diff, merge, resolve conflicts•Hard at first, pays off in big projects

Page 33: Hardcore PHP

CODING PRACTICES

Learn & do Testing•Unit tests• Test DB data (Fixtures)•Mock Objects

• Integration tests•Acceptance tests

Page 34: Hardcore PHP

WEB PRACTICES

•Learn Kung-fu:•HTTP protocol basics• Some Javascript•Minimal CSS• Robots.txt• Favicon.ico• Sitemap.xml• Cookies• Encoding•Web Security basics

Page 35: Hardcore PHP

WEB PRACTICES

•Minimize HTML, CSS, JS•Google closure Compiler•YuiCompressor•Firebug•Use tools to detect improvements:•PageSpeed (Firefox/Chrome)•YSlow (Firefox)•MySpace Performance Tracker (IE)

Page 36: Hardcore PHP

WEB PRACTICES

•Use the client to store data•Cookies (4KB max)•LocalStorage (HTML5)•Global scoped Javascript variables (AJAX only)• Javascript Datasources (Tuenti AJAX)

Page 37: Hardcore PHP

WEB PRACTICES

•If you don’t need realtime, be lazy•Lazy loading•Lazy deletion• Job queues instead of realtime operations

Page 38: Hardcore PHP

THE END

¿Questions?

http://dev.tuenti.comhttp://jobs.tuenti.com