Introduction to PHP OOP

This is the first in a series of articles about using PHP to do objected oriented programming, or OOP. They were originally published elsewhere but are no longer available at that location, so I’m reposting them here.

Since the introduction of PHP 5 in 2004, PHP has had an object model worthy of that description and became a truly modern language for use on the web. Earlier PHP scripts would have been of the kind where, to quote from Alice’s Adventures, you would “Begin at the beginning, and go on till you come to the end: then stop.” Nowadays that very procedural approach is less common in PHP, so this article takes a look at some of the basic object oriented features available in the language and shows some examples of using them with code examples.

Continue reading

Confident Coding: San Francisco

While I’m in the US in a week or so, I’ll be joining a stellar lineup at Confident Coding on October 20th in San Francisco. This is a by-women, for-women event to let us get together in a safe space where there are no stupid questions, and try to cover those tricks that it seems like everyone knows, but we all had to learn sometime!

Personally I’ll be speaking about git and also about SSH and things that are not FTP, and anything else I get asked about on the day. The variety of skills in the speaker lineup of this event, organised by the lovely @estellevw, is frankly imporessive and I can’t wait to meet all the speakers and attendees! I’m not often in the US at all (I’m a very reluctant traveller and I’m actually there for ZendCon the week after) so this is a rare opportunity for me.

The event is open to everyone, but if you don’t identify as female and you want to attend, please bring with you someone who does – and either way you can make use of my discount code! Simply buy a ticket, entering LORNA20 at the checkout for 20% off the ticket price.

Hope to see you there :)

PHP for Drupalistas

There’s an exciting new venture coming up soon – something I’ve been working on with Emma Jane for a while (yes, Lorna Jane and Emma Jane, we know) – it’s called phpfordevelopers.com. From the site:

This spring Emma Jane and Lorna Jane were chatting about PHP and Drupal and workshops and came to the conclusion that Drupal developers were not necessarily equipped for Drupal 8. With all of the Drupalisms in the Drupal code, it can sometimes be difficult to implement code that is both a Drupal best practice and a PHP best practice. While there are many workshops on how to teach PHP developers how to Drupal, there were no workshops teaching Drupal developers how to PHP. Until now!

My theory is that most developers working with CMSes like Drupal think they don’t know much PHP … but of course they actually know quite a lot! The newer versions make more use of OOP and new PHP features, but nothing that’s really rocket science (although the symfony components are very nice). This course is a chance for us to give a more solid grounding to those skills that developers just pick up along the way, and give some time to master those skills in a safe environment. Continue reading

Skills Allied to PHP

This post is mostly about a tutorial I will be delivering at PHPNW on October 5th in Manchester, UK, and why I think a tutorial that contains no PHP belongs at a PHP conference

phpnw12 logoIn October, I’ll be delivering a tutorial at the mighty PHPNW Conference which contains very little PHP. Why? Because I think, as developers, it’s our other professional skills that suffer. As a consultant, I work with lots of different teams, and it is very rare for code to be the problem (and the one time it was, it wasn’t the only problem!).

In web development, our biggest challenges are not writing code, we can do that. But getting the code safely from one place to another, with many people’s work preserved, having our platform(s) correctly configured and understanding how to use them, making use of the tools in the ecosystem which will help us improve the quality of our code; these are the big challenges we face, and that’s why I proposed this workshop and why I think all these topics are important. Continue reading

Validating Email Addresses in PHP

A very quick snippet today because I’ve told two people to use this approach in the last few days and both of them told me they didn’t know about it. How to check if an email address is valid in PHP: use one of the Filter functions, like this:


$email1 = "nonsense.something@dottiness";  // not a valid email
$email2 = "[email protected]";  // valid email

$clean_email1 = filter_var($email1, FILTER_VALIDATE_EMAIL); // $clean_email1 = false
$clean_email2 = filter_var($email2, FILTER_VALIDATE_EMAIL); // $clean_email2 = [email protected]

The Filter extension was new in PHP 5.2, but is one of the unsung heroes of the language. It’s rare for me to ever describe one approach as the “right” way to do something – but for validating data, Filter really is excellent, offering both validating and sanitising filters and generally making it super-easy to clean up incoming variables. Many of the frameworks offer equivalents, and I’m sure many of those are wrapping this too.

Installing PEAR Packages Offline

As with most tools that work really well, I know very little about PEAR. I mean, I use it all the time, and I love it for getting all the extensions installed that I need for the work I do. But I’ve never made a PEAR package, or channel, and I’ve been happy to leave all those things in the hands of the smart people who have created what we have today.

However I’m now in a situation where I might need to install PEAR packages with a connection that may or may not be working, and I’m not sure exactly which packages I might need, so I wanted to know whether I could use PEAR as my packaging tool even when I wasn’t able to reach the usual channels. And guess what? I can! Continue reading

PHP 5.4 Benchmarks

Today I’m giving my first ever talk at OSCON – about PHP 5.4 (I’ll also be giving my second ever talk at OSCON, about RESTful services; it’s a busy day!). My talk includes some benchmarks which I thought I’d also share here, mostly because I like pretty graphs – and this one is pretty:

graph comparing performance of PHP versions
Continue reading

Bit.ly API: Bundles and Short URLs

I am a huge fan of bit.ly and use their tools for a wide variety of different things. They recently did a big relaunch with some lovely new features, which are for the most part pretty good, but which are inaccessible in places. In particular, it seems that there aren’t any short URLs for the bundles – which is annoying for me as I use that feature a lot!

To get around this, I used their API to make a page which lists my bit.ly bundles, and creates shortlinks for each of them (once you’ve created a shortlink for a given URL once, bit.ly just re-uses the same ones the next time you ask to shorten the same URL, so this is less silly than it sounds).

In case the code is helpful, I thought I’d share. Continue reading