For instance, if we have a function that takes an integer, we should enforce that.

function foo(integer $some_int) {
  if ($some_int > 3) {
...

Comments

thenorman’s picture

... and on failure there should be a return type of false, null, or something to indicate bad data and provide a clear path to error or error control.

nevets’s picture

I do not believe you can declare the type of PHP argument or variable.

thenorman’s picture

You can declare types in PHP, but normally most coders do not. Still, the idea of this to force integrity of the variables and provide a path for corrections. Int could be replaced with is_digit().

cha0s’s picture

Sorry, my original example was incorrect, I guess int and string aren't supported in type hinting http://php.net/manual/en/language.oop5.typehinting.php

However, my point stands that we should use this when we can.

nevets’s picture

I not convinced the extra overhead is worth it.

Island Usurper’s picture

Status: Active » Closed (won't fix)

Since we only require PHP 5.0, we can't specify array as the type hint, and that's what are passed around most of the time. I think there are also several places where we pass in an object or FALSE (as opposed to NULL) because that's what is returned by various load functions, and type hinting would force us to change that.

If (when?) we get to the point where we are designing a new API that uses different types of objects in various ways, then there's no problem with using type hinting then.