Explicit type checking or implicit type hinting?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Explicit type checking or implicit type hinting?

Post 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 :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Explicit type checking or implicit type hinting?

Post 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.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Explicit type checking or implicit type hinting?

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Explicit type checking or implicit type hinting?

Post 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.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Explicit type checking or implicit type hinting?

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Explicit type checking or implicit type hinting?

Post by alex.barylski »

Personally I don't think the redundancy is worth it.
Touche...I needed that little shove thanks :)
Post Reply