1. P P os' uk
H D en Sc
t
This is a presentation about PHP.
If you are looking for a fight, leave now.
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
t
I'm 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 I'm known for anything on the Internet it's Beef Taco.
Just google Beef Taco and it's the top item.
I kid you not.
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
t
PHP Sucks.
The language itself has some fundamental flaws that
crept in early in it's 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 it's held
onto backwards compatibility through a lot of them.
Zend (4/5) is a decent engine, but the old cruft is lame.
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
t
Why else does PHP suck?
Well, it runs everywhere. It's 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 don't
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. Don't
listen to Rasmus.
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
t
I 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.
Don't blame everything on the language.
So how can we write better PHP?
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
t
Learn the language.
It sounds simple, but I bet there is lots of stuff that even
good PHP programmers don't 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. 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
t
Test your code.
This is a huuuuge deal. There are good testing tools
for PHP, use them. I like SimpleTest, but it's a
personal choice, and an easy one.
TDD makes you think, something PHP doesn't always
make you do.
Something else that unit testing does is force you to
separate code. PHP is it's own template language,
so it's super easy to mix business logic with
presentation. Don't. Testing will help.
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
t
No live edits. Stay off of production.
I admit, I don't use version control on some stuff. One
off tools and pages, wordpress blogs, stuff that
doesn't 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. 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
t
Share your code, and use shared code.
You don't have to come up with your own version of a
lot of things.
But don't just blindly copy code or use any old module.
Look at the code and make a judgment call. It's okay
to fork or rewrite if it's in bad shape, but then share it
back to the community!
If you are writing a library, be compatible. If it's
generic, there are naming conventions like PSR-0.
(I admit I am bad at being compatible)
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
t
Have 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 else's project, use their
style before you make a pull request.
It's polite, it's clean it's essential.
I use a variant of MediaWiki's conventions.
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
t
Other 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 it's often easier to
try it in the shell than to go look it up on php.net.
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
t
Actually READ the code you use.
This one is a big one. Read other people's code, the
good and the bad.
Identify what you like and what you don't like about
other people's code.
Try out new things you find in other peoples code.
And don't 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. 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
t
Use a framework.
A framework will make you organize your code within a
set of conventions. Which conventions don't matter
as much as the fact that they exist.
I recommend not Zend because it frightens me.
Learn your framework well.
Read it's core, submit patches, be involved.
Here are few examples of frameworks:
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
t
I'm a Kohana user.
It's not all roses though, there are no migrations either,
which is gross.
Development moves fast, and breaks api a lot. It's
managed with versioning though, and there is plenty
of backporting and bug fixes.
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 ary'o'= '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
t
Another cool one is Lithium.
It's very “leading edge” and uses namespaces like they
are going out of style.
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
t
Finally I included a Sinatra and Bottle style framework
that I wrote.
I wouldn't necessarily recommend using Skunk, but it's
a good example a little, single file framework with an
interesting API and feel.
17. S.
o.
.
P P os' uk
H D en Sc
t
So.
Those are my thoughts on how to write better PHP.
As for the language itself...
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
t
It's getting better.
5.4 is on it's way and it's bringing some fixes for major
pain points.
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
t
Please be gentle.