php hooligans

Embracing the PHP philosophy

A fractal of bad design?


There is no design

no single dictator or design committee


PHP assimilates

and nothing gets thrown away

what some call quirks

- needle haystack or haystack needle -

- to under_score or not -

others may call 'backwards compatibility'

learn to use code completion


P.S.

array function? -> needle haystack

string function? -> haystack needle


design patterns

MVC,factory,singleton,observer,

listener,strategy,decorator,active

 record,gateway,proxy,adapter


cute, so you're a hotshot programmer

now what does this

AbstractSingletonProxyFactoryBean

actually do?

keep it simple


don't use design patterns just because you can


don't name your functions after generic design patterns


design patterns are a code smell


we're not programming java here

these are not the types you are looking for

php isn't fuzzy about types

avoid type hints and embrace duck typing

everything is a string

remember this and PHP will not surprise you
too often

PHP is designed for web applications

variables passed through GET or POST are strings

so PHP assumes anything is a kind of string

use === and !== to avoid surprises

object oriented

it's not what you think



Avoid objects


Never use objects where simple types suffice

Objects are shared state
shared state leads to complexity


Arrays in PHP are fast and flexible
They are passed by value
with copy-on-write

e.g.


$info = $treeNode->info; $treeNode->cd('..'); $info2 = $treeNode->info;
this leads to problems
unless  node->cd() returns a new object 


$info2 = \mylib\tree::cd($treeNode, '..')->info;
This makes it explicit

AVOID INHERITANCE


embrace composition and encapsulation


use 'final' and 'private'
(some hooligans disagree)


call it dependency injection and you are cool too


large class hierarchies are a code smell

keep it small

when in doubt, leave it out

don't overthink, build what you need now


The best code is no code


unit test only find bugs in what they test

the number of bugs per line of code is a constant



so by writing half the code you avoid half the bugs


keep the php spirit

conform to the language


work with standard types if at all possible


emulate them if not

( toString(), ArrayObject, etc.)


keep the learning curve low

finally: steal

there is a lot of useful code out there


even if you don't like the quality, chances are they fixed problems you haven't found yet


http://phptrends.com/

http://packagist.org/


do check the license though

php hooligans

By Auke van Slooten

php hooligans

  • 2,298