Page 1 of 1

Explicit type checking or implicit type hinting?

Posted: Tue Jun 09, 2009 2:59 pm
by alex.barylski
I'm re-working a framework and trying to decide on the best practice which to follow:

1. Rely on native PHP errors for things like invalid type reference, etc
2. Check for types in methods which accept objects as parameters, etc and throw exceptions or trigger custom errors

I like the latter only because the PHP errors don't always translate clearly into 'Alex' :P whereas my own messages are usually much easier to comprehend as they take more context into play.

The downside is, explicit means more lines of code, which in this case is not much, it's still more and I believe less is more...

Incase I am missing something, share with me your thoughts, please and thank you :)

Re: Explicit type checking or implicit type hinting?

Posted: Tue Jun 09, 2009 3:04 pm
by alex.barylski
Basically, I am explicitly testing for conditions which are already carried out by PHP itself, so long as you use type hinting.

Code: Select all

function(MyType $type)
{
  if(!($type instanceOf $type)){
    throw new Exception('Some long message');
  }
}
At least I am pretty sure that if someone passed in an object that was not at least a descendent of MyType PHP would complain, which is pretty much what my conditional test is doing on in exception form.

I dislike the idea of using hints but at the same time they offer many neat opportunities, like dependency metrics, etc.

Re: Explicit type checking or implicit type hinting?

Posted: Tue Jun 09, 2009 5:04 pm
by allspiritseve
I've not tried this yet, but isn't there a way to override PHP's error handling to throw exceptions instead of triggering errors? That would be ideal, but then again if you're working a framework that might be making too many assumptions.

Re: Explicit type checking or implicit type hinting?

Posted: Tue Jun 09, 2009 5:14 pm
by alex.barylski
I suppose you could override the default error handler and throw an exception but that isn't quite what I'm asking. :P

I prefer explicit programming practices...but in this case it seems redundant...considering the engine would trigger an error if I just used type hinting...

The again...I also check to ensure methods exist before I call them and really that is redundant as well, as in either case an error is triggered (a core error) or a custom one letting me know the method doesn't exist.

It's not like I'm actually recovering from a error like that, in which case an explicit test would make a difference, allowing me to re-query the object.

Re: Explicit type checking or implicit type hinting?

Posted: Tue Jun 09, 2009 5:24 pm
by allspiritseve
PCSpectra wrote:It's not like I'm actually recovering from a error like that, in which case an explicit test would make a difference, allowing me to re-query the object.
If you're recovering from it, then I'd say just use type hinting. Personally I don't think the redundancy is worth it.

Re: Explicit type checking or implicit type hinting?

Posted: Tue Jun 09, 2009 6:07 pm
by alex.barylski
Personally I don't think the redundancy is worth it.
Touche...I needed that little shove thanks :)