Extending SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I posted a while ago about problems with extending SimpleXML (which I intend to do so I can add automatic microformat support). Well I think I’ve come up with a solution.

I’ve created a class called ExtendXML that is completely extendable (see the other post for an explanation of why SimpleXML isn’t extendable) that provides all of the functionality of SimpleXML.

The initial call is similar to using SimpleXML (except I made the functions static class methods instead):

ExtendXML::load_string($xmlString, [$class, [$simpleClass]]);` `ExtendXML::load_file($xmlFile, [$class, [$simpleClass]]);`

The $class argument contains the name of the class that will be returned. This should extend ExtendXML and defaults to ExtendXML.

The $simpleClass argument contains the name of the SimpleXML class that will be passed to SimpleXML functions. This should extend SimpleXMLElement and defaults to SimpleXMLElement.

The object returned can then be used the same as a SimpleXMLElement object.

I haven’t tested it much or documented it yet, but it supports child tags (returning objects are of the class ExtendXML or whatever you specified), attributes and xpath expressions.

ExtendXML

PHP5, OOP, ArrayAccess, SPL, programming, code, tutorial, microformats, XML, SimpleXML

Google Adwords CPM campaigns

Oliver Brown
— This upcoming video may not be available to view yet.

I’m experimenting with a CPM site-targetted campaign on Google Adwords and so far the results are promising. I have two campaigns advertising the same site one with a max CPC of 20¢ and the other with a max CPM of $2.00 targetted to a single site.

So far the CPC campaign is producing more traffic, but it’s costing me on average 18¢ per click whereas the CPM campaign is only costing me 4¢ per click.

I think because of my very selective targetting I’ve managed to find a site that has visitors looking for what I’m advertising, but not offering competition (hence a higher CTR and lower effective CPC).

PHP5 book review

Oliver Brown
— This upcoming video may not be available to view yet.

http://rcm.amazon.com/e/cm?t=galaxia-20&o=1&p=8&l=as1&asins=1590593804&fc1=000000&lc1=0066cc&bc1=ffffff&lt1=_blank&nou=1&IS2=1&f=ifr&bg1=ffffff&f=ifr

PHP 5 Objects, Patterns, and Practice

Put simply, after reading this book I finally “got” how to use OOP usefully in a web application. Too many OOP tutorials use examples of objects too abstract and literal to be useful (like animals or shapes). This book takes real world examples and explains what sort of things really should be objects and how to use them.

The book is basically split into three sections, as the title suggests. The first part is a very quick overview of OOP and the new features of PHP5. This is thankfully short (and as such this book is aimed at people with a fair bit of PHP experience).

The second section is the largest and basically goes through the most common design patterns and explains situations you’d use them in.

The third section is less focused on OOP and covers some practical considerations including version control and unit testing.

I would say this is probably the best book I’ve bought (except my first Perl/CGI book) and I recommend it for anyone who has to yet to be convinced about PHP as a robust OOP language.

Issues with extending SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I wrote a post about my attempts to extend SimpleXML to support microformats. Well it’s not as it easy as it seems.

You can’t define new properties of a class extending SimpleXMLElement. Well you can define them but they don’t work - when you access them they always return a SimpleXMLElement object (or actually an object of whatever class you defined).

I’ve tried overriding __get and __set and neither work so the only thing I can think of is to create a new class that delegates most of it’s work to SimpleXMLElement.

Ads by Google

Oliver Brown
— This upcoming video may not be available to view yet.

An image ad for Adventure Quest (an RPG) keeps appearing at the top of my blog in the Google spot. But, it doesn’t show the “Ads by Google” link. It also has a little cross in the top right hand corner that makes the ad disappear…

WWE too real?

Oliver Brown
— This upcoming video may not be available to view yet.

There was an bit on the news today about UFC. Apparently brutal cage fights are being banned in various states in America because they’re too dangerous. At the end the reporter said that even where it is banned, you can buy DVDs via mail order and the internet with footage of the action. He then held up the “WWE Bloodbath - The Most Incredible Cage Matches”.

WWE!? I’m sure that WWE would be offended to be accused of the brutality of UFC and UFC would be annoyed about being compared with fake pro wrestling. Not only that but the DVD contains matches from 1975 to 2002. Early WWE (or WWF as it was) was even sillier in the past.

Sims 2 is complicated

Oliver Brown
— This upcoming video may not be available to view yet.

Unrelated to almost anything I’ve written, I just thought I’d suggest anyone remotely interested in god-games to buy Sims 2 now. It has managed the same thing as the Grand Theft Auto series. There is a reasonably structured game in there now to guide you in your actions, but the game is still wide open if you want to do something else entirely.

Most of this structure revolves around the new wants and desires your sims have. They’re tasks or events that reward or penalise you and are decided by your Sim’s aspiration. The funniest is the “romance” aspiration which has wants such as “Have 2 loves at once” or “Make out with 3 different Sims”…

Risk variants

Oliver Brown
— This upcoming video may not be available to view yet.

Ever heard of the board game Risk? If you haven’t then unfortunately I’m not going to be the one to tell you about it so go away now :).

If you have heard it, this post is going to mention a quick variation on the original rules I tend to play with friends. There may be more in the future.

The modification we play most of the time is to Risk cards themselves. We found suddenly receiving 8 or 10 troops in a country of your choice too powerful (how anybody plays with the progressive-risk-card-reinforcement rule I don’t know). So we decided that you only got the two reinforcements in the country on the card. The alteration is that it applies even the country is an oponent (we call it the Paratrooper rule). If you have paratroopers they just attack that country like normal.

Silliest Google conversion

Oliver Brown
— This upcoming video may not be available to view yet.

If you didn’t know, Google can do clever unit conversions. Example conversions include:

1 mile in km 4 weeks in days 4.9 lightyears in au

You can even use compound units:

55 mph in m/s 20 N/(m^2) in Pascals

In fact you can use arbitrary compound units. The silliest I managed to find is:

2 great gross speed of light in bakers dozen furlongs per year

A great gross is 12 gross (a gross is 144). A bakers dozen is 13. A furlong is 220 yards.

Microformats extension to SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I’m writing a PHP5 object that extends SimpleXML to automatically add various microformats. So you could do, for instance, the following:

$xml = SimpleXML\_load\_file('test.xml');
foreach ($xml->hEvent as $event) {
    print $event->original();
}

This would print the original HTML of every hEvent found in the HTML document. XML, PHP, PHP5, microformats, hCard, hReview, hCalendar