Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Tuenti - Hardcore PHP - UAM
Search
Kartones
November 24, 2011
Programming
1
200
Tuenti - Hardcore PHP - UAM
@ Universidad Autónoma de Madrid (2011)
Kartones
November 24, 2011
Tweet
Share
More Decks by Kartones
See All by Kartones
Building Autonomous Agents with gym-retro
kartones
0
42
Python static typing with MyPy
kartones
0
64
High-impact refactors keeping the lights on
kartones
0
63
Remote Work
kartones
0
82
Geospatial CSV Imports Hidden Complexity
kartones
0
46
Intro to GameBoy Development
kartones
0
94
Myths & The Real World of OpenSource Development
kartones
0
45
CartoDB Tech Intro
kartones
0
44
Copy Protection & Cracking History
kartones
0
120
Other Decks in Programming
See All in Programming
ErdMap: Thinking about a map for Rails applications
makicamel
1
760
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
950
watsonx.ai Dojo #6 継続的なAIアプリ開発と展開
oniak3ibm
PRO
0
180
return文におけるstd::moveについて
onihusube
1
1.4k
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
6
710
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
140
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
8
1.4k
良いユニットテストを書こう
mototakatsu
13
3.6k
Beyond ORM
77web
11
1.6k
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
15
2.5k
AHC041解説
terryu16
0
450
선언형 UI에서의 상태관리
l2hyunwoo
0
270
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
580
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
Git: the NoSQL Database
bkeepers
PRO
427
64k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.1k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.5k
Code Reviewing Like a Champion
maltzj
521
39k
Being A Developer After 40
akosma
89
590k
Transcript
None
None
INTRO • Senior Frontend Engineer @ Tuenti • C#/.NET background,
now a bit of PHP knowledge •
[email protected]
• twitter.com/kartones
AGENDA • The Goal • Shared Hosting to Tuenti-like scale
webs • Typical PHP • PHP Practices • Coding Practices • Web Practices
THE GOAL
THE REAL GOAL Avoid the PHP joke
THE REAL REAL GOAL Build quality PHP code
SHARED HOSTING Internet Frontend + DB
FIRST SPLIT Internet Frontend DB
MORE FRONTENDS Internet Frontends DB
CACHING TIME Internet Frontends DB Cache
MORE CACHING Internet Frontends DB Cache
MASTER/SLAVE DBS Internet Frontends DBs Cache
TUENTI (OVERVIEW) Internet Farm 1 Farm 2 Farm N Others
Frontends Caches DBs
TYPICAL PHP •HTML + PHP script blocks + DB Queries
•If lucky, separated into ¨functions¨ and templates (PHPBB, Wordpress…) •No Object Orientation
TYPICAL PHP News 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>
TYPICAL PHP News 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>
TYPICAL PHP News 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>
TYPICAL PHP News 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>
TYPICAL PHP function crop_string($string) { if (strlen($string) > 30) {
$string = substr($string, 0, 30) . “…”; } return $string; } $text = crop_string(“developers,developers,developers,developers”);
TYPICAL PHP class 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”);
PHP PRACTICES •PHP 5.3 (or the newest stable version) •Object
Orientation •Namespaces / structured source code tree
PHP PRACTICES •Layered code •MVC is typical and good Controller
Model View
PHP PRACTICES •Breaking loops is ugly for($i = 0; $i
< count($items); $i++) { if ($items[$i] == searchedItem) { break; } }
PHP PRACTICES •Break-free $found = false; for($i = 0; $i
< count($items) && !$found; $i++) { if ($items[$i] == searchedItem) { $found = true; } }
PHP PRACTICES • Try to keep memory usage low •Less
memory, more concurrent PHP processes • unset() • ini_set(“memory_limit”,”8M”);
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
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
PHP PRACTICES • Avoid non testeable objects class Game {
private $player1 = new GamePlayer(); private $player2 = new GamePlayer(); public function Play() { // Logic that uses $player1 & $player2 } }
PHP PRACTICES • Create testeable objects class 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 } }
PHP PRACTICES • Defensive Programming • defined() • isset() •
class_exists() • method_exists()
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
CODING PRACTICES Learn & do Testing •Unit tests •Test DB
data (Fixtures) •Mock Objects •Integration tests •Acceptance tests
WEB PRACTICES •Learn Kung-fu: •HTTP protocol basics •Some Javascript •Minimal
CSS •Robots.txt •Favicon.ico •Sitemap.xml •Cookies •Encoding •Web Security basics
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)
WEB PRACTICES •Use the client to store data •Cookies (4KB
max) •LocalStorage (HTML5) •Global scoped Javascript variables (AJAX only) •Javascript Datasources (Tuenti AJAX)
WEB PRACTICES •If you don’t need realtime, be lazy •Lazy
loading •Lazy deletion •Job queues instead of realtime operations
THE END ¿Questions? http://dev.tuenti.com http://jobs.tuenti.com