Class type hinting in PHP 5

Double quote marks 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 there's no real way to enforce the type of objects that are passed between methods Double quote marks

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.

JavaScript Haiku

Double quote marks I'm quite interested in the idea of code being poetry, but just generally not interested enough to do anything about it... Double quote marks

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);

PHP 5: Class hinting

Double quote marks Class hinting is where the class of object to be passed into a method is specified in the function call. Double quote marks

Class hinting is where the class of object to be passed into a method is specified in the function call.

e.g.

abstract class User
{
 protected $logState;

 public function User()
 {
  $this->logState = new LogState()
 }

 public function setLogstate(LogState $logState)
 {
  $this->logState = $logState;
 }
}

This code will throw a runtime error if the variable passed into the setLogstate method is anything but a LogState object.

The benefits of this are twofold:

  • Errors in code are trapped at an earlier point in runtime. If the type was not checked then $this->logState may contain, for example, a string or other variable type and the mistake not caught until much further through processing making it more difficult to locate the source of the error.
  • The code is self-documenting as it is obvious to anyone reading the setLogstate method what class of object $logState will contain

The hint may not just be a class, it can also contain the name of an interface. Hence a method such as:

function saveSomething(Saveable $objectToSave)
{
//Do things with $objectToSave
}

will allow through any object which implements the Saveable interface.

Review of ‘The Mapper’, Spectrum Adventure Game

Double quote marks I've found that the adventure game I wrote for the ZX Spectrum when I was 17 got a pretty good review in Your Sinclair. I never noticed at the time as I went off to University (and there certainly weren't a flood of royalties - it was the end of the Spectrum heyday). Double quote marks

I’ve found that the adventure game I wrote for the ZX Spectrum when I was 17 got a pretty good review in Your Sinclair. I never noticed at the time as I went off to University (and there certainly weren’t a flood of royalties - it was the end of the Spectrum heyday).

The game was published by Zenobi Software, who published a large number of adventure games on the Spectrum, and written using the Professional Adventure Writing System (PAWS). I think I made £60 out of it… Still, I was proud of it at the time and seeing it reviewed in a national magazine is very satisfying.

You can read the review here on the Your Sinclair tribute site