PHP Best Practices

There’s a new thread started over at phpwomen.org about best practices for programming with PHP, which will make interesting reading for anyone looking to improve their skills. I’ve been really brave and contributed one of the articles, PHPDoc: Comments are your friends.

People are all contributing a little knowledge about the area they feel comfortable with and even at this early stage the results are impressive!

PHP Throwdown Icon

As you may remember we participated in the PHP Throwdown a while ago. Well Elizabeth has sent out the icons to competitors who successfully got through the 24 hours and submitted something which vaguely conformed to the rules. You can see ours on the left :)

Does PHP know itself?

I’ve run into a problem with a framework I’ve been contributing to. It has a bunch of objects, which look a lot like rows in database tables. So there are pages and each page object is a row in the “pages” table in the database, with a primary key called “page_id”. There are also sections and each section object is a row in the “sections” table in the database with a primary key called “section_id”… you get the picture.

So there’s a parent class called “Model” which figures out the magic table/primary key names in the constructor and can then do a variety of tricks on the generic data set. The various object types such as page then extend this “model” and use its methods. Which was going quite well until I tried actually calling the methods.

Statically.

Because I figured that even if the code was inherited from somewhere else, PHP would know which class had called it in the first place and so I’d be able to reference “self” and know what was happening. So wrong!!

Its a feature of PHP that in fact the self() object for a method called statically will always reference the place the code is actually executing. So in order for me to statically call a method which is going to return me an array of instantiated objects of whatever type I called the method against (still with me??), I must first instantiate an object of that type and call it in the usual way.

So in case you’re wondering what the variables $pointless_page and $pointless_section are doing at the top of my script then now you know! I love the little quirks that programming languages have …

Open Office Custom Colours

Yesterday I was working on a document in Open Office and wanted to colour the background of some cells to match a diagram I had created1.

The choice of colours wasn’t great and there wasn’t a “more colours” button. After a few minutes of growling I went to Tools -> Options … and clicked on the colours menu item. Lo, and behold.

Not only can you add colours but you can completely alter the available colours for the grid. I can imagine this being a really useful tool for adding presets to assist with corporate branding or other situations where continuity is important and you don’t want to pick a non-corporate shade of green at any point when creating a document!

That’s my nice-software-touch of the week, what’s yours?

1 Dia is my diagramming program. Its interface is great, but its unlike anything else so expect to have to work at using it to start with.

Tiny Vim on Ubuntu

A new installation of Ubuntu exists at work and I’ve discovered that by default, it doesn’t come with real vim. The clue is in the error message:


E319: Sorry, the command is not available in this version: syntax enable

E538: No mouse support: mouse=a

To get things back to normal I needed to install vim and then alias it correctly. Using


which vim

I discovered that /usr/bin/vim was being used – this symlinks to /etc/alternatives/vim and that in turn links back to /usr/bin/vim.tiny. So I installed vim:


sudo apt-get install vim

and then changed the symlink in /etc/alternatives to point to /usr/bin/vim.basic instead – much better :)

PHP register_globals is ON

I started writing PHP when I was at University, in 2001 or thereabouts. Having checked the dates it looks like I did technically write PHP before PHP 4.3 was released and register_globals went out of fashion. I don’t recall it though and obviously its been a while!

Today I was trying to debug a script which uses register_globals on, virtually for the first time. Its a new working environment for me and a situation where support is still being provided for some legacy systems. Trying to track where values are coming from, or might be coming from, was quite mind-bending. Its like having voices inside my head while I’m trying to concentrate on something!

There’s more where that came from so if anyone has any suggestions for debugging a language where the variables seem to come from nowhere, or can remember how to work with PHP with register_globals turned on, I’d be grateful :)

UK PHP Conference: The Roundup

As promised, here’s a more in-depth update on our trip to the PHP London Conference last week. I’m sure there are others who will write more fluently and accurately about the talks, but here’s what I brought home from it.

Cal Evans: My First Mashup

Cal is a personal hero of mine, as I said earlier, so I was really looking forward to his talk. He did a simple mashup of two APIs which tracked a UPS package to its destination on a Google Map. He used APIs from both companies and had a working demo to show.

Although he showed JSON and XML data formats, he mostly seemed to be using XML and had some good things to say about the SimpleXML functions now in PHP 5. For making the API calls he used script.aculo.us.

From the talk I came away with a clear picture of how a mashup goes together and a sense of confidence that when a project comes along for me that needs it I’ll just dive right in. I saw the whole API idea in action when writing PlayTAG for the PHP Throwdown. Cal also pointed us at http://www.programmableweb.com/ which looks like a good resource.

Simon Laws: Web Services

Simon was talking about a project he and his colleagues have been working on to create consumable services, which followed on nicely from Cal’s talk. The project is a PECL module and their page is at www.osoa.org, its good to see business contributing so nicely to the community.

The idea of the project is that you can write some class-based functionality and then expose that to be called as a service by some external script. It was interesting and the talk was well-prepared with demonstrations and code snippets all at different points along the development that Simon walked us through. My personal favourte moment was him typing “phuk” rather than “phpuk” into a sample input … perhaps that just my twisted sense of humour. One thing that really impressed me was that they had spent a lot of consideration about making different formats available and providing various bindings for accessing the services. Some of that information about what’s available for what type of service is held in phpDocumentor-style comments at the moment but it could easily be generated as a static companion file or something further along the line.

Best of all was getting to catch up with Simon and his colleague Caroline (a PHP Women friend of mine) at the event after the conference and ask them a bit more about their job. They were both interesting and friendly people.

Kevlin Henney: Objects of Desire

Kevlin was very brave to come and speak as he’s not a PHP-er himself most of the time (or so he claimed). He was talking about the PHP implementation of Object Oriented Programming in comparison with other languages including Java and C++. His talk was based around a series of questions and he was a very entertaining speaker.

His perspective of coming from a wider background than just the single language was very interesting and I did feel that it gave a much more balanced perspective on the topic. The talk was based on answering a series of questions and this worked very well. In particular he was talking about unintended properties of objects and illustrated this by showing that his bottle of cough mixture had the property “throwable”, as it was quite small and aerodynamic … I did warn you he was entertaining :)

Rasmus Lerdorf: Fast and Rich Web Applications with PHP 5

Rasmus Lerdorf is the godfather of PHP and a nice guy. I’ve heard him on podcasts, own some of his many books, and have even corresponded on forums with him but not seen him in the flesh before so this was interesting. He was talking about PHP5 and then went on to answer quite a few questions which was very interesting. At the beginning there was a problem with the wireless in the venue and he raised a smile around the room by putting up a slide which showed photographs of:

  • a ship in the Mediterranean
  • a field in India
  • a mountain in Cambodia

And then explained that these are all places where he has given talks and wireless internet access had been arranged … we were in Central London!!

Rasmus talked about the movement to PHP5 and adopter rates and barriers. He recommended using the Filter functionality in PHP for all input variables and gave some good examples. His other recommendations included making use of the static keyword where appropriate to prevent unexpected usage or extension of methods, and also use of the -l switch for checking PHP4 code prior to deployment for PHP5.

Conclusion

For an even costing £50 and organised by volunteers it was an excellent day! There was coffee available at every turn and lunch was provided, they certainly knew how to take care of geeks. Attending the pre- and post-event socials definitely led us to get more out of the whole experience and meet some very interesting people. It was well worth the effort and I’d certainly recommend this to anyone interested in PHP for next year.

New Addition To The Family

I’m proud to announce that we are now the owners of a MacBook! Or rather, Kevin is the proud owner of one and I am being friends with it too. It arrived yesterday and is called Edoras. All our computers need names because we have too many to be able to refer to them otherwise!

I’m not too familiar with Macs but I’m looking forward to getting to know this one, not least because its little and sexy. Software recommendations are most welcome, if anyone has any favourites.

UK PHP Conference: The Highlights

I’ve been away for a couple of days at the UK PHP Conference in London. Due to the unique way that train ticket prices work here, it was wildly cheaper to travel a day either side of the day we actually wanted to be in London for which allowed us to take in some sights and socialise with some other PHP-ers.

I’ll post more about the conference itself but for now I’d like to leave you with the highlights.

  • Hearing Rasmus Lerdorf speak … he was interesting and entertaining and managed to out-geek everyone there all in one go.
  • Getting to say hi to Rasmus later on (I was probably quite star-struck but never mind! I do own a few of his books so I’m allowed to idolise slightly)
  • Getting to say hi to Cal Evans, editor of Dev Zone and all-round good guy. He helped phpwomen.org in its infancy. In addition Cal is now my hero as he gave me a pack of the PHP Playing Cards I have coveted for so long! He just had a few sets in his hand after his talk and gave them to bystanders, talk about the right place at the right time.
  • Meeting a phpwomen.org friend in real life for the first time :)