Working with PHP and Beanstalkd

I have just introduced Beanstalkd into my current PHP project; it was super-easy so I thought I’d share some examples and my thoughts on how a job queue fits in with a PHP web application.

The Scenario

I have an API backend and a web frontend on this project (there may be apps later. It’s a startup, there could be anything later). Both front and back ends are PHP Slim Framework applications, and there’s a sort of JSON-RPC going on in between the two.

The job queue will handle a few things we don’t want to do in real time on the application, such as:

  • updating counts of things like comments; when a comment is made, a job gets created and we can return to the user. At some point the job will get processed updating the counts of how many comments are on that thing, how many comments the user made, adding to a news feed of activities … you get the idea. Continue reading

PHP and Git Training Course Dates

I’ve had a little flurry of enquiries about training lately, so I thought I’d mention the courses I have coming up, as especially the PHP ones are topics that I don’t run public classes on all that often. At the time of writing I have some space on all of these classes: Continue reading

Zend Certified PHP Developer 5.5

Yesterday I updated my previous ZCE certificate to the Zend Certified PHP Developer qualification (the new ZCE for PHP 5.5 also got a new name). Since the ZCE 5.3 exam is no longer available and I work with various clients to prepare their teams for these certifications, it was important to me that I keep my own certification up to date. Now I’ve done that, I’d like to share some resources for others doing the same thing.

Sample Questions Pack

One really important step in preparing for this exam is to get an idea of what kind of questions you might be asked – in terms of the format of the questions and the topics. I have a pack of 70 questions which I use when delivering ZCE preparation courses, but I also sell it separately and it is now updated for PHP 5.5

This pack is now available from https://leanpub.com/zce

As well as questions, this includes answers with detailed explanations of how those are reached and links to further reading. There is also some advice about the format of the exam and what to expect on the day itself.

Links Bundle

The PHP Manual is fabulous, but sometimes you need a more conceptual explanation. I maintain a bundle of links to blog posts or other tutorials on the various topics involved in ZCE, which you may find helpful to dip into for your own study:

http://lornajane.net/zce-links-collection

If you find any broken links, or have any resources you think should be included, just let me know. I intend for this to be a living document that we can share.

Revision Flashcards

My advice for cramming for ZCE is always the same: you need to recap all areas of the manual but focus especially on strings and arrays, because while there will be an average number of questions on these topics, it’s common to see strings and arrays used in questions that are really about function scope, or inheritance, etc.

For my own revision, I created flashcards by taking the PHP manual and making them into double-sided PDFs that I could cut up and use (you could do this with a single-sided printer, print the odd pages first and then put the paper through again – for duplex printers beware that you need to choose “short side”).

Here are the String and Array flashcards that I used for myself (they’re not perfect, but I found them useful so if you want to download them, you can. The main omission is that I stripped < and > characters which makes for interesting string comparison documentation).
Hopefully some of these resources will help you prepare for your own professional certification – good luck :)

Setting Multiple Headers in a PHP Stream Context

Last week I tried to create a PHP stream context which set multiple headers; an Authorization header and a Content-Type header. All the examples I could find showed headers built up as a string with newlines added manually, which seemed pretty clunky and not-streams-like to me.

In fact, you’ve been able to pass this as an array since PHP 5.2.10, so to set multiple headers in the stream context, I just used this:

<?php
$options = &#91;"http" => [
    "method" => "POST",
    "header" => ["Authorization: token " . $access_token,
        "Content-Type: application/json"],
    "content" => $data
    ]];
$context = stream_context_create($options);

The $access_token had been set elsewhere (in fact I usually put credentials in a separate file and exclude it from source control in an effort not to spread my access credentials further than I mean to!), and $data is already encoded as JSON. For completeness, you can make the POST request like this:



Video Course on Learnable: OOP PHP

I’m delighted to announce that my new video course on Object-Oriented PHP is now available on Learnable! It’s very much an introduction, aiming to cover WHY objects are so cool as well as how to declare and use one. The course is a mix of video (filmed in my kitchen, welcome to my home everyone!), screencast, a couple of exercises for you to try, and also plenty of sample code to download. If you are just looking to get started with OOP, or know someone who is, then hopefully this will help you out.

On a related note, I’m also doing a Sitepoint “Talk with the Experts” session on 11th April (early morning UK time, as a special treat for everyone in Europe and further east, that doesn’t happen often!). There are more details here: http://www.sitepoint.com/forums/showthread.php?1012242-Talk-Object-oriented-PHP-with-the-Experts and I hope you can join me then.

PHP 5.4 Timezone Error Message on Ubuntu

Quick post because this tripped me up the other day: When you use a vanilla ubuntu 12.10 “Quantal Quetzal” installation, it will come with PHP 5.4, which is excellent news. However the default php.ini doesn’t set the timezone, so you will see an error like:

It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.

These have been warnings in earlier versions of PHP, but as of PHP 5.4, the date.timezone ini setting must be set correctly, using the continent and place – for me that’s “Europe/London”, like this:

date.timezone = "Europe/London"

If you see these errors, don’t panic, just add the line above to your php.ini.

PHP at FOSDEM 2013

In case you haven’t been following, FOSDEM is a Free Software/Open Source weekend event held every year in Brussels – it’s free to attend and it’s huge! It’s actually a network of smaller events, and this year that includes a whole day of PHP on the Saturday (2nd February 2013). Our schedule is excellent, taking in frameworks, extensions (tutorial from Sara Golemon!), nginx, APIs and a few other goodies. Also, this lovely one-day PHP conference is in the wider context of an event that is packed full of otehr excellent open source technology sessions – see the full schedule for what else is on offer. I love these open source events for the opportunity to dip into not-PHP topics, and I hope to see you in Brussels! Come along, be geeky, and bring your friends :)

9 Magic Methods in PHP

This post forms part of 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. Previously in the series was an introduction to OOP in PHP, in two parts

The title is a bit of a red herring as PHP has more than 9 magic methods, but these will get you off to a good start using PHP’s magic methods. It might be magic, but no wands are required!

Continue reading

A Little More OOP in PHP

This post forms part of 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.

This post follows an earlier entry introducing the basics OOP and what that looks like in PHP. This time around we’ll look at some more advanced concepts and some more practical examples of building code, covering use of constructors and how to add access modifiers in to control how calling code can operate on your objects. We’ll also show off how to create static methods and properties and, perhaps more importantly, illustrate applications of these features.

Continue reading

PHP at FOSDEM: Call for Papers

There’s an excellent open source conference that happens every year in Brussels in February, called FOSDEM. It consists of some main tracks, plus a series of sub-rooms, where various technical communities are given some space to use for whichever talks they choose; the schedules are centralised so that people can pop in and visit any talks in any room that looks interesting. This year, for the first time, this includes a “PHP and Friends” room – I’ll be organising this and I’m looking for your input, please.

Basically, we need to get some great submissions, so that when we come to choose the schedule (and it is only one track, one day, there’s only a few slots available), we can put together something really fitting to showcase PHP for a wider audience than a PHP conference. Selection will be done on the basis of talk topic, abstract and length in the first instance – we’ll only take into account the actual speakers when we’re curating the final list.

Key things you need to know:

  • link to call for papers form (google forms)
  • Event is 2nd (and 3rd, but the PHP room is on the 2nd) of February 2013, at ULB campus Solbosh in Brussels, Belgium
  • No expenses will be covered by the event
  • There’s also a Call for Stands if you have a project that you would like to represent there
  • If you’re not speaking, come and join us anyway!