P P os uk                   H D en Sc                         tThis is a presentation about PHP.If you are looking for a f...
Wh ae o?                     o r yu                        Jh H b s                        on ob                        - ...
P P uk                       H Scs                  TP A Y N K D TYM                  _A MA I E U O A I                   ...
P P uk                          H Scs                               LwB re T Et                               o ar ro n y ...
Ia w i                     cn re                         t                   t r lcd i                   e ie o en        ...
Mai I oSc                     k g t tuk                      n N                   Lan h Ln u g                   e r te a...
Mai I oSc                    k g t tuk                     n N                     TsY u C d                     et o r o ...
Mai I oSc                     k g t tuk                      n N                     N L E DT                      O I EI ...
Mai I oSc                      k g t tuk                       n N                     S ae B sae                      h r...
Mai I oSc                      k g t tuk                       n N                         H v ASy                        ...
Mai I oSc                     k g t tuk                      n N                       T Te E L                       r h ...
Mai I oSc                    k g t tuk                     n N                        Ra C d                         ed o ...
UE           S           A       F A WOR       R ME   K                   (i: o Zn)                   Hn N t ed           ...
Ue F m w r                    s A r e ok                        a                     - MV                     H C        ...
Ue F m w r                    s A r e ok                        a          nmsaeapcnrles           aepc potolr;          c...
Ue F m w r                    s A r e ok                        a                         Suk                          kn ...
S.                    o.                     .      P P os uk      H D en Sc            tSo.Those are my thoughts on how t...
I G t g ee                       t ei B tr                         t                        s   n   t         54          ...
Ds so                     ius n                      c i                  Qusos                    etn?                   ...
Upcoming SlideShare
Loading in...5
×

PHP Doesn't Suck - Notes

1,662

Published on

These are the notes I used for my presentation "PHP Doesn't Suck" at Barcamp Omaha 2011

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
1,662
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

PHP Doesn't Suck - Notes

  1. 1. P P os uk H D en Sc tThis is a presentation about PHP.If you are looking for a fight, leave now.
  2. 2. Wh ae o? o r yu Jh H b s on ob - ee p r t a C er D vl e a Wh t he o - jhb s @m o b -ts /iu. m jhb s hp: ghb o / o b t/ t c m -t: vl t ce r/ hp /e e ah. g t/ v c o P P os uk H D en Sc tIm John Hobbs.I make fancy websites at What Cheer.I have a twitter and a github and I blog but no one reads it.If Im known for anything on the Internet its Beef Taco.Just google Beef Taco and its the top item.I kid you not.
  3. 3. P P uk H Scs TP A Y N K D TYM _A MA I E U O A I M_ g t l s = g tp ( e c s ) > e ye) _a ( t P P9 6 F4 4 8 1 2A 6 - A 0 1 C 4 H E 5 83 - 2 - d -7 9 0 A 0 A F 2 D 1 0 sr & ary ot $ r ) ( a w wppa nscm w . sd es o h . P P os uk H D en Sc tPHP Sucks.The language itself has some fundamental flaws that crept in early in its life and have not been excised yet, mostly for compatibility issues.Crappy error messages.Inconsistent naming conventions.Stupid “features”In place array sorts.There is a decent chunk of these, phpsadness.com is a good place to read and track them.PHP has a weird history of full rewrites, but its held onto backwards compatibility through a lot of them.Zend (4/5) is a decent engine, but the old cruft is lame.
  4. 4. P P uk H Scs LwB re T Et o ar ro n y i r We k o m ny a C m ui t G o lC p - s o g -o y at e P e NH I “ t edI i yu ifd h to r o erw I h n tn o wli t yu h m go n n e hk ln a s a f m w r h sae yuie n a gaai m lr e ok a svd o t a d g rvtn la m o a d o ed p i a ee po ut -am s n yu n u wt b tr rd c” R s u h t . hp/t sed rcm aci s3 - e orm w r- P Cf m w r.m t: o .rofo /r v / 8 h- -a e ok H - -a e ok tl t / yl . he T nf P MV r h P P os uk H D en Sc tWhy else does PHP suck?Well, it runs everywhere. Its billed as an easy language to learn, fast. And it is, if you want to write crappy code.The community is, compared to Python and Ruby, fragmented and not sophisticated. Testing is a big problem here. Ever used a phpt?Because PHP is everywhere, you can Google for anything and get some code. Too often people dont read it, they just copy-paste. So they lose a chance to learn and also integrate crappy code.There also seems to be a serious not-invented-here syndrome in PHP. This includes Rasmus. Dont listen to Rasmus.
  5. 5. Ia w i cn re t t r lcd i e ie o en rb ayaga e n l ug. n - Me P P os uk H D en Sc tI can write terrible code in any language.The point is that the quality of the code has a lot to do with who is writing it, and how much they understand.Dont blame everything on the language.So how can we write better PHP?
  6. 6. Mai I oSc k g t tuk n N Lan h Ln u g e r te a g a e hp /h . t a u l t : p pn / na t/ em P P os uk H D en Sc tLearn the language.It sounds simple, but I bet there is lots of stuff that even good PHP programmers dont know about PHP.Has anyone here ever written a __clone method?How much of the manual have you read?Have you ever compiled an extension? Written one?
  7. 7. Mai I oSc k g t tuk n N TsY u C d et o r o e h p/ww .m l et r/ t: t / ws pe sog i t . ht: w .h u i e t / wwp p n . / p/ td ht: q .h . t reet h t / ap pn / i -sp p p/ e w tt . P P os uk H D en Sc tTest your code.This is a huuuuge deal. There are good testing tools for PHP, use them. I like SimpleTest, but its a personal choice, and an easy one.TDD makes you think, something PHP doesnt always make you do.Something else that unit testing does is force you to separate code. PHP is its own template language, so its super easy to mix business logic with presentation. Dont. Testing will help.
  8. 8. Mai I oSc k g t tuk n N N L E DT O I EI V S ( ot ) m sy l C P TA O WOR S I E T P P AI R N S K F WI H N H P P os uk H D en Sc tNo live edits. Stay off of production.I admit, I dont use version control on some stuff. One off tools and pages, wordpress blogs, stuff that doesnt really matter.If you are writing an app or a library, you should be using version control and running deployments after all your tests pass.Capistrano gets along fine with PHP, and if you would rather you can use phing and other tools.PHP tempts you to edit live in production. Resist.
  9. 9. Mai I oSc k g t tuk n N S ae B sae h r & e h rd Gt b iu h S - hp/ i ulo /h j ) P R0(t : t y rcm y 6y d t /n . d P P os uk H D en Sc tShare your code, and use shared code.You dont have to come up with your own version of a lot of things.But dont just blindly copy code or use any old module.Look at the code and make a judgment call. Its okay to fork or rewrite if its in bad shape, but then share it back to the community!If you are writing a library, be compatible. If its generic, there are naming conventions like PSR-0.(I admit I am bad at being compatible)
  10. 10. Mai I oSc k g t tuk n N H v ASy a e tle hp / w .ei i. gwk Maul o i _ovnos t: w wm d wko / i/ naC d g cnetn t/ a ir i : n i P P os uk H D en Sc tHave a style you stick to.Being clear and consistent in your code is essential, no sloppy formatting.Bad style is endemic of google-copy-paste code.If you are working on someone elses project, use their style before you make a pull request.Its polite, its clean its essential.I use a variant of MediaWikis conventions.
  11. 11. Mai I oSc k g t tuk n N T Te E L r h RP y jhbsCrei:$pph mob@odla~ hs Satn pp trig h tp o ep t seisrcin &faue ye h r hl o e ntutos etrs pp eh "hhi" h> co O a!; O hi h a! pp icue"etpp; h> nld ts.h" pp wo(; h> ha) ll fntos1 o, ucin!! pp h> jhbsCrei:$ mob@odla~ ht: w .h s.r/ t / wwp ph g p/ o P P os uk H D en Sc tOther languages have a great REPL.PHP, not so much.Luckily we have a good one from Facebook, written mostly in Python actually.Get it and give it a shot.When you forget argument ordering its often easier to try it in the shell than to go look it up on php.net.
  12. 12. Mai I oSc k g t tuk n N Ra C d ed o e ( c d goh ra g a e! I l i te ln u g s nu n ) P P os uk H D en Sc tActually READ the code you use.This one is a big one. Read other peoples code, the good and the bad.Identify what you like and what you dont like about other peoples code.Try out new things you find in other peoples code.And dont be just a PHP programmer.Read and write some other languages, Python, Ruby, whatever.I am convinced the more well read you are, in any language, the better you will be as a programmer.
  13. 13. UE S A F A WOR R ME K (i: o Zn) Hn N t ed t P P os uk H D en Sc tUse a framework.A framework will make you organize your code within a set of conventions. Which conventions dont matter as much as the fact that they exist.I recommend not Zend because it frightens me.Learn your framework well.Read its core, submit patches, be involved.Here are few examples of frameworks:
  14. 14. Ue F m w r s A r e ok a - MV H C - sf hl r Ue le es u p -u e fx l Sp rl ie eb - ra m dl ss m G e t o u syt e e - ra m dl G et o u s e hp /oaarm w r og t: khnf e ok r/ t/ a . P P os uk H D en Sc tIm a Kohana user.Its not all roses though, there are no migrations either, which is gross.Development moves fast, and breaks api a lot. Its managed with versioning though, and there is plenty of backporting and bug fixes.
  15. 15. Ue F m w r s A r e ok a nmsaeapcnrles aepc potolr; casPssotolretnsihucinCnrle { ls otCnrle xed ltimatootolr pbi fnto idx){ ulc ucin ne( rtr aryo= a il= ot; eun ra(fo > br, tte > Pss) } } hp /tfm / t: l i.e t/ i y h P P os uk H D en Sc tAnother cool one is Lithium.Its very “leading edge” and uses namespaces like they are going out of style.
  16. 16. Ue F m w r s A r e ok a Suk kn / Iiilz / ntaie $ =nwSuk) s e kn(; / Adarue / d ot $-gt s>e( h(<ae), /i/nm> fnto (&s $ae=“ol”){ ucin $, nm wrd $-bd =“el $ae" s>oy Hlo nm!; } ); / Rni! / u t $-rn) s>u(; hp: ghb o / hb sSuk ts /iu. m j o b/kn t/ t c m P P os uk H D en Sc tFinally I included a Sinatra and Bottle style framework that I wrote.I wouldnt necessarily recommend using Skunk, but its a good example a little, single file framework with an interesting API and feel.
  17. 17. S. o. . P P os uk H D en Sc tSo.Those are my thoughts on how to write better PHP.As for the language itself...
  18. 18. I G t g ee t ei B tr t s n t 54 . -t: w wpp e m na e/ n ug. p .as h hp / w . . t a ul nl ga e o 5t i. p t/ hn/ / a o r tp ( as > x s T i = Min) rt i -t: s . p e v w cv w rvi &eio= 1 6 1 hp /v p . t i v?i =eio rvi 33 4 t/ n h n / e e sn sn (hrAry yt : “” > b ] Sot r Sn x[a = “” ) a a -t: nw . p e ppn ra /5 9 hp /e s h. t h.t nl 5 2 3 t/ p n / ie s (V = g) SN > it P P os uk H D en Sc tIts getting better.5.4 is on its way and its bringing some fixes for major pain points.
  19. 19. Ds so ius n c i Qusos etn? i C m et o m n? s P P os uk H D en Sc tPlease be gentle.

×