Drupalcon notes: Objectifying PHP

domesticat's picture

(No, PHP, your ass doesn't look fat in that. Honest.)

  • Classes have interfaces, which declare which functions/methods it supports.
  • A good object is atomic. It does one thing, and one thing well.
  • Focus on making a good data object (pizza, cat, node). They don't have functionality. They just exist.
  • Behavior objects are active, configurable, and do things.
  • Be smart about how many dependencies you hard-code in.
  • Every function call or static method is a hard dependency!
  • Good objects are opaque: it doesn't care what happens on the other side of the function. It should just know that it will get back a string, integer, whatever
  • Drupal has a lot of circular dependencies, and this is a problem. Separation needs to happen. Doing so makes the code more testable and swappable.

So what should we actually do?

  • Modules should become namespaces. Get out of the hook business. (We think.) Turning hooks into objects is not the way.
  • What about the major systems, like theme, menu?
  • Magic callbacks: lazy OOP without class

Good practices:

  • Minimize extending
  • Always provide an interface
  • No exposed properties (if you expose a property, you don't know who has mucked with it)
  • Don't be private (learn the difference between 'protected' and 'private' - properties should always be protected. methods should be public or protected depending on their needs)
  • Constructor injection (don't just call out for a new object - pass info to it.)

Good for learning:

  • martinfowler.com has a good article on constructor injection.
  • Zandstra, Mark: PHP Objects, Patterns, and Practice (2008) Ignore everything it says about PHP 6
  •  planetphp.com
  • garfieldtech.com - there's an OOP section

Post new comment

User login

Recent comments

  • Anonymous 3 days 11 hours ago [view]
  • Charli 2 weeks 3 hours ago [view]
  • quiltmom anna 3 weeks 3 days ago [view]
  • rslatkin 3 weeks 5 days ago [view]
  • Donna 3 weeks 6 days ago [view]

Search

Hello, anonymous!

If you're seeing this, you're not logged in. A lot of content here is only visible if you're logged in, and comments by anonymous users are held for moderation. Consider getting an account to save yourself some frustration?

domesticat.net

is the home of Amy Qualls-McClure since 2000. She is a Drupal / quilt geek in Huntsville, Alabama. One spouse, two cats, no kids, lots of opinions.

Public account for work and Drupal stuff: Private account for friends and personal life:

me on plurk me on drupal.org my music habits on last.fm my photos on flickr my bookmarks on del.icio.us my bookmarks on pinboard.in Amy Q. on foursquare what I'm reading

Some content is locked. Copy these links AFTER logging in for a query string giving you full feed-reader access:

Atom feed, entries RSS feed, entries RSS feed, comments