Class type hinting in PHP 5

PHP 5’s OO structure is way ahead of PHP 4 and has so many ‘proper’ object oriented concepts built into it that writing well-structured code is much easier than ever. The problem that PHP faces (and may always face) is that being a loosely-typed language (click here for an explanation of the differences between PHP and other languages) there’s no real way to enforce the type of objects that are passed between methods, or to overload methods in the sense of the word used by Java, C# and other OO languages.

One thing that PHP5 does allow, however, is what they call ‘class type hinting’. This allows you to specify the type of object which is passed into a method and, if a variable is passed in incorrectly, then a runtime error is generated. E.g.

class Foo
{
  public function __construct(Bar $bar)
  {
  }
}

$foo = new Foo($bar);

This will throw an error if $bar doesn’t contain an object of class Bar.

This is a step forward in that at least you get a warning about your code breaking closer to where it actually happened than, perhaps, five methods down the line when you actually try and do something with the incorrect object, but there are a couple of parts of the implementation that I still don’t really like.

The first problem is that the error message only gives the line number of the method that’s called, not the callee so there’s still some back-tracking to find the source of the error. Still, beggars can’t be choosers…

The other is that some of the built-in types don’t seem to be recognised. Therefore you can specify your own classes by name but using ‘String $myString’ or ‘Array $myArray’ doesn’t wash with the interpreter. This means you have to leave those untyped, which kind of defeats the purpose in some cases.

The other issues are really to do with a wish-list. What would be really good would be:

  • Overloading: to be able to define more than one method of the same name.
  • Specifying the return type from a method. E.g. Foo $foo = $new Foo(); Bar $bar = $foo->makeBar();

I don’t know whether that’s ever going to be possible given loose typing but I don’t think PHP 5 will be regarded as a true rock-solid OO language without them.

Spam: I just don’t get it.

Actually, I get a lot of it. Spam comes flooding through old e-mail in a
constant stream. The thing I don’t get is: what is the spammer getting out
of it? I mean, most of the e-mails at the moment are for one of:

  • Rolex’s: as if they’re genuine. Yeah, right.
  • Various pharmaceuticals: I don’t even know what these are. What the hell
    is a ‘soft tab’? Why is it better than not being a soft tab? (I really don’t
    want to know, by the way.)
  • Mortgage approvals: hang on, what a great idea, I’ll hand over the deeds
    to my house to someone in another country from a company I’ve never heard
    of! What a waste of time.
  • Cheeaap Softwarrreees!: OEM Photoshop! What a bargain! If such a thing existed, I might just be tempted. But then again, probably not.

Okay, I’ve seen an
article on the economics of spam on The Register
that reckons only 50
in every million people need to respond to make money, but 50 seems a high
number for these obviously useless e-mails. I guess someone must be making
money out of it, but who?

The best theory I can come up with is that the people who are doing the
spamming are selling their services to people who are probably even more
gullible than the intended victims of spam: those people who think they can
send a few million e-mails and make their internet fortune. The market for
get-rich-quick schemes is always busy…

Incidentally, as an excellent way to catch spam I’ve been using Robin Keir’s K9 Spam Filter, which is
free and extremely good.

JavaScript Haiku

There are a few sites on the web which will generate haiku’s using a set of code rules. I think I can lay claim to have written what could be the world’s first (and only) haiku actually in JavaScript. I’m quite interested in the idea of code being poetry, but just generally not interested enough to do anything about it…

It even has a title which relates to its content, which is:

Fear of tomorrow

now = new Date();
day = now.getDate();
alert(day+1);

W32/Sober.j@MM Virus Virus spam

I seem to be suffering from a huge number of people with my address in their book sending me viruses pretending to be ‘important message’ attachments. What a pain… It’s okay when I’m in the office as the spam filter gets all of them, but if I’m out and about and want to connect on the mobile phone then I can end up downloading 100 headers.

Brussels freelancers beer night

Some of us from www.freelancers.net have just come back from a great couple of nights in Brussels. I’ve never been to Belgium before but it was a great laugh. We stayed in the ‘Hotel Catalonia’, which was a pretty good place, somewhere in the Art Nouveau quarter. It was pretty short on art nouveau, however…

The full attendees were:

  • Karl Bunyan (me)
  • Deborah Causton
  • Dave Edwards
  • Susan Engel
  • Lindsey Hill
  • Luke Perman
  • Paul J White

I’m not going to put the full details here (even if I could remember them…) but a few photos are below:

Luke pointing at a statue

Black and white shot of me and Luke

Lindsey, Susan and Luke

Dave and Susan

Paul, not too drunk yet

Deborah, on a rare occassion when she isn’t hiding

Me with a glass of Kwak. I really did turn into Mr Hyde later that night.

Susan, Luke and myself on our way to the Eurostar.

Me hiding. Sweatshirt sleeve/mask of zorro.

Susan, Dave, Paul and thumbs

Dave goes for his guns/beer

Hot chocolate self-assembly kit before

Hot chocolate self-assembly kit after

Group photo: Susan, Luke, Lindsey, Dave, Me (and Paul behind the camera)

Susan and Linsey

It rained a bit while we were there…

We know exactly where we are, honest.

Me, Luke and beer. Such a rare sight to see together.

A student beer-and-lorry-fest. No idea what this was all about.

Princess Susan

Mealtime

Another shot of someone drinking (spot a theme?)

Dave and Deborah, before Deborah had a chance to hide

An overturned car that Paul spotted coming out of an underpass

The window of our hotel. Fascinating.

Lindsey, Luke, Susan and Paul (from left to right)

X-Entertainment – Spider-Man Reviews Crayons! Part 1 of 3

“Spider-man Reviews Crayons” is hilarious. Definitely worth a read of the whole thing…
X-Entertainment – Spider-Man Reviews Crayons! Part 1 of 3.

Basically, Spiderman has signed up to a deal to review a full set of Crayola crayons. The pictures are absolutely superb… I don’t know how they managed to get a plastic Spiderman figure to have an expression on his face, but he does.

Review of Kerala Indian Restaurant, Oxford Circus

I had the good fortune to visit a South Indian restaurant called Kerala,
near Oxford Circus, last night. Located in an unlikely place for an original
restaurant it was a real surprise.

I’m a big curry fan, and Kerala is certainly a treat. The menu is nothing
like the majority of Indian restaurants (read ‘Curry Houses’) you find;
there were a couple of the ‘standard’ dishes (a biryani, something
korma-looking) but otherwise it was generally a lot more authentic and based
(I was told) on Keralan cuisine. My choice was a dosa to start with and a
mutton curry main course. Both were very good indeed, and the meat was much
more tender than any of the lamb I’ve had down Brick Lane. I tried a few of
the other dishes and the roast duck was particularly good.

The drinks list was fairly standard, except instead of “Kingfisher” or
“Cobra” the Indian beer was “Adi adi”. I’d never heard of it, and it had an
interesting taste with floral overtones. Very unusual for a lager.

The service was good throughout, although I’m convinced the waiter will
have aching cheek muscles at the end of each night from so much grinning.
Prices were above the usual curry house, but the quality matched it and it
was still a reasonably cheap meal at around £15 a head

I would absolutely recommend Kerala. It’s a great find, and to find such
good quality in the centre of London is absolutely fantastic. The menu is
very broad and would be excellent for vegetarians, as well as having a wide
range of fish dishes.

Kerala Restaurant, 15 Great Castle Street, London, W1W 8LT. Nearest tube
Oxford Circus

Review of Kerala on London-eating

Review of Kerala at
Curry House Reviews

Medieval Total War, Italian tips

Okay, I’ve been playing far too much Medieval Total War lately. It’s
only my first campaign and I started playing as the Italians mainly
because:

  • It didn’t seem too hard, or too easy
  • Being English seemed to easy, and too obvious
  • I had delusions of being able to start my own in-game
    renaissance

So, having brought Italy from it’s small beginnings to conquering most
of Europe I have the following advice

Don’t expand too quickly

I’ve made that mistake a couple of times. As well as being difficult to
manage, your much more likely to have a rebellion occur when you’re
already overstretched. The artificial intelligence in Medieval Total War
seems to keep an eye on how vulnerable you are and will spring a
surprise on you (e.g. an old faction re-emerging) at the time you’d
least like it to happen.

Occupy provinces for a few years with a large force before moving
on

It takes a while for a region to settle down. It’s tempting once
you’ve ‘broken through’ to go on the rampage, but the provinces you
leave behind will pretty soon revert to their old ways

Don’t upset the Pope

In retrospect, I did upset the Pope far too early in the game by
attacking the Hungarians. It meant he declared a crusade against me and
then half the catholic world declared war. I think there is probably a
better way to use religion within Total War than I have so far.

If you take over Rome, watch out for the Pope

If you do upset the Pope then the best thing to do is invade Rome and
destroy the faction. That voids all excommunications and crusades. The
problem is, every few years there’ll be a really big rebellion usually
involving about 1000 highly trained troops. The good thing about these
is that the Pope usually makes a really bad general so the army is often
easy to rout. Which brings me to:

If you have to fight a large force of knights…

…use plenty of missile power, and keep your troops in formation.
It’s much better in this situation to be defending, so as soon as you
see an uprising fill the province with troops and try to fight it down
in the same turn it attacks. That way you can just occupy a hill
position and rain crossbow bolts and arrows down on the troops.

Don’t chase routing troops

Unless you see the ‘The Enemy is fleeing the field’ message.
Reinforcements may be over the hill and being caught out of formation is
very bad.

Use the right troops

Spearmen or feudal seargants are very good against cavalry, as long
as the cavalry don’t attack from the flank. The Medieval world is very
vicious…

Watch out for the Sicilians too

Despite invading and conquering Sicily there were a number of
resurgences of the Sicilian or rebel factions.

Build a lot of ports and ships

As a Meditteranean nation, the Italians are often fighting on a
coastal region. If you have a string of ships and a port at the start of
the journey then you can move armies around the map almost instantly.
This makes defending places like Rome and Sicily much easier. You should
still keep large armies in these places, though, as a deterrent.

Build ports in places that have a tendency to rebel

This especially applies to Rome. It may be easy to push forces in
from the Meditteranean, but when things hot up you need to redeploy them
equally as quickly.

Build Royal Palaces

The Italians can train Royal Knights. These are excellent against
lesser trained troops. Remember not to get them bogged down against
spearmen especially, though. Charge in, then charge out. During a
battle, keep them on the flanks so they can run outside the enemy and
round the back.

Make the most of artillery when defending

It’s not much use when you’re attacking, but for defending a hilltop
a couple of catapults can scare the shit out of most enemies. Medieval
peasants especially generally won’t be prepared for stones on their
heads.

Occupy the seas

Very important for the Italians, and also good for trade. Try to
maintain sea-going superiority. This gives tremendous flexibility when
attacking somewhere such as Spain.

Establish an Eastern border with some good castles

I’ve stopped short of invading Poland so far and have some fairly big
castles built up along the border. So far, this has put them off any
idea of invasion

Only fight one war at a time

As with Don’t expand too quickly, if there’s too
much going on then you’ll be seen as an easy target. This can also be
used to your advantage: weaken any of the other medieval factions and a
rebellion may pop up. This is especially likely to happen if you manage
to kill a faction leader.

Develop Italian Infantry

You need to be quite technically advanced in terms of castles and
weaponry but Italian Infantry make very good fighters in virtually any
battle situation.

Go for technology over fast growth

A well-trained force can often beat another army of double its size.
My Italians have just fought off the Egyptians in a war with odds of
2-1. Remember, there’s a limit on the number of soldiers on the field at
any time with Medieval Total War so you don’t need to fight them all of
at once, just one wave at a time

Keep an eye on archers’ ammo during battle

If you’re fighting a large battle with reinforcements available then
watch how your archers arrow stocks are going. If they’re low and
there’s a lull in the fighting then withdraw them and call on the
reinforcements.

Harass footsoldiers with mounted crossbowmen

Just watch out for any heavy cavalry around and get ready to withdraw
them

Leave some regions underdeveloped

Not ideal, but it’s better to spend the money wisely in one area than
to spread it too thinly. Some provinces are really only any good for
developing farmland. Tuscany, for example.

Concentrate on creating different warrior types in each
province

E.g. Once an area is able to generate spearmen, try to make them
better spearmen. The next province can be given the task of making
archers (for instance) – give the region a speciality. This is
especially useful where you want to build shipyards as galleys take so
long to construct; there’s no point in being able to build every kind of
troop in one region if the throughput is low. Sometimes you may be under
attack and need to build hundreds of troops very quickly

Spread the generals around

It’s helpful to have a high (3 or more) star general near anywhere
that might start a war. That said, it’s also best to leave a provinces
governor at his home. Go for a combination of dread and acumen in
governors and leave the good generals free to fight the wars.

Merchants and trading posts are most effective in port regions

And almost worthless anywhere else.

Well, there you have it. The best collection of Medieval Total War
tips I can manage from my experiences so far.

More tips: