For instance, if we have a function that takes an integer, we should enforce that.
function foo(integer $some_int) {
if ($some_int > 3) {
...
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
Comment #1
thenorman commented... 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.
Comment #2
nevets commentedI do not believe you can declare the type of PHP argument or variable.
Comment #3
thenorman commentedYou 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().
Comment #4
cha0s commentedSorry, 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.
Comment #5
nevets commentedI not convinced the extra overhead is worth it.
Comment #6
Island Usurper commentedSince 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.