PHP Anti Patterns
(and how you’re guilty of using them)
Josh Butts
PHP Benelux 2015
Slide 2
Slide 2 text
About Me
• Over 10 years of “pro” PHP
• VP of Engineering at Offers.com
• Austin PHP organizer
• Taught several PHP classes
• github.com/jimbojsb @jimbojsb
Slide 3
Slide 3 text
No content
Slide 4
Slide 4 text
Preface
• This talk is 100% my opinion
• You may disagree with some of it
• I disagree with some of it
• Find me after and tell me why I’m wrong
• PHP is ever-changing. This talk could be wrong 6 months from
now
Slide 5
Slide 5 text
What is an Anti-Pattern?
An anti-pattern (or antipattern) is a common response
to a recurring problem that is usually ineffective and risks
being highly counterproductive.
(so says Wikipedia)
Slide 6
Slide 6 text
What is an Anti-Pattern
Slide 7
Slide 7 text
What is an Anti-Pattern
• Tend to appear in OOP
• Contradict good OOP patterns
• In PHP, there are plenty of non-oop examples too
• Why does the PHP let us do it then?
Slide 8
Slide 8 text
Everyone Is Guilty
• Road to hell is paved with good intentions
• Lesser of the evils
• Good software design doesn’t (necessarily) make money
• Blame PHP
Slide 9
Slide 9 text
NIH Syndrome
• Everyone should write their own X
• Don’t actually use it
• Re-inventing an already documented bad
idea
Slide 10
Slide 10 text
Singletons
You might as well just use global variables
Slide 11
Slide 11 text
Examples
• Public statics
• Service locators
• Bad DI Containers
Slide 12
Slide 12 text
Why would you use a singleton
• Cross-cutting concerns
• Configuration options
• State-passing in a legacy system
Slide 13
Slide 13 text
Example Singleton Class
Slide 14
Slide 14 text
God Objects - The Worst Singletons
Zend_Controller_Front
• The core request handler for Zend Framework 1.x
• Singleton with a public getInstance() method
• Has a reference to EVERYTHING
• Change the response code from your view
• Disable exception handling from your models
• Really, very, extremely convenient
Slide 15
Slide 15 text
Your First PHP Job
Slide 16
Slide 16 text
Your Second PHP Job
Slide 17
Slide 17 text
Database IPC
Your database is not a job queue
Slide 18
Slide 18 text
Database IPC
• We’ve all done this
• Probably at your second PHP job
• MySQL is your everything
• But it’s easy and it works…
Slide 19
Slide 19 text
Database IPC
• Sure, it works on your local dev laptop
• I promise this will fail you in production
• Why: Locking, polling, IO, query cache
• MediaWiki
Slide 20
Slide 20 text
But I would never database IPC!
I bet you would.
Slide 21
Slide 21 text
How do you fix it?
• Use a message or job queue
• There are a ton of good ones
• Beanstalkd
• RabbitMQ
• Gearman
• Kafka
• Amazon SQS
Slide 22
Slide 22 text
Interface Bloat
In reality, no one will build an alternate implementation
Slide 23
Slide 23 text
Interface Bloat - How it starts out
• Step 1 - Write Code
• Step 2 - Publish on Github
• Step 3 - Tweet your new lib
• Step 4 - Fame?
Slide 24
Slide 24 text
Interface Bloat
• Why do you use interfaces
• Who is the audience of your code
• Is there a reason to extend what you wrote?
• Slippery slope
• Java Envy
Slide 25
Slide 25 text
Pheanstalk\Pheanstalk
Slide 26
Slide 26 text
No content
Slide 27
Slide 27 text
Enough with OOP
Lets talk about about plain old PHP
Slide 28
Slide 28 text
Mixed returns
• Check your data models
• Check your database
• E_FATAL
Slide 29
Slide 29 text
Common DB Example
Slide 30
Slide 30 text
Slightly better - return array()
Slide 31
Slide 31 text
Better still
Slide 32
Slide 32 text
Extract
Not even once…
Slide 33
Slide 33 text
This is just a bad idea all around
Slide 34
Slide 34 text
Error Handling
Throw a damn exception!
Slide 35
Slide 35 text
trigger_error()
• From hard to impossible to handle during runtime
• Just use exceptions - we even have finally now
• Still 17 occurrences of trigger_error() in the
Wordpress codebase
• 22 in Joomla
• Surprisingly only 3 in Drupal 8, and the seem to be
outside the core
Slide 36
Slide 36 text
Sequential Coupling
Your objects require their methods to be called in a certain order
Slide 37
Slide 37 text
Sequential Coupling
• Requires setters after constructors
• Failure to inject dependencies
• Requiring external state management
• valid() to check state?
• Poorly written API clients
Slide 38
Slide 38 text
Don’t abuse PHP’s goofy booleans
You’ll be sorry way down the road
Slide 39
Slide 39 text
Rating Example
Slide 40
Slide 40 text
Rating Example
Slide 41
Slide 41 text
Include abuse
You’ll thank me later
Slide 42
Slide 42 text
Include means parse and eval
My Blog
Last updated:
Slide 43
Slide 43 text
static_homepage.html
Welcome to my blog
Slide 44
Slide 44 text
Trouble
Welcome to my blog
Slide 45
Slide 45 text
Include means parse and eval
My Blog
Last updated:
Slide 46
Slide 46 text
Quick Hits
One bullet says it all
Slide 47
Slide 47 text
Quick Hits
• Writing your own password hashing
• Having more than one require call in your code
• $_SESSION is not for global storage
• Changing the memory_limit
• Running PHP <5.5
• Making assumptions about “goodness” of built in language
functions
• @
• E_ALL
Slide 48
Slide 48 text
The Elusive While-Break
I’ve only ever seen this once, so maybe it’s not a pattern…
Slide 49
Slide 49 text
No content
Slide 50
Slide 50 text
method1();
$obj->method2();
break;
}
Slide 51
Slide 51 text
No content
Slide 52
Slide 52 text
What I didn’t mention, on purpose
• Not writing tests
• Not using a framework
• Using goto
• Mixing PHP and HTML