I’m building my own framework, and I’m trying to pass a parameter to a static method. For some reason, the parameter is not getting passed. Here is the code:
Front.php:
if(URI::get(0) === "")
URI.php:
public static function get($index)
{
die($index);
if(!filter_var($index, FILTER_VALIDATE_INT)) {
throw new Exception('You must supply an integer index for the URI segment');
}
return self::$uri[$index];
}
At first I was getting an exception, so I added in the die statement to make sure the $index was actually getting passed correctly. Apparently it’s not because when the script exits, nothing gets printed out for the index.
I am using php 5.3.1.
This is rather odd, as it should work ; after testing this portion of code :
I get this output :
Which indicates the static method did indeed receive the parameter (and I see no reason why it shouldn’t, actually).
As a sidenote, you are ending with this portion of code :
Quoting the manual page for
exit(which is the same as die) (emphasis mine) :You are using PHP 5.3, which is a more recent version than 4.2 ; and, in your case,
$statusis an integer — which means it is perfectly normal to not have anything displayed, with the code your posted.And, to finish : if you remove the
die, your code ends up doing this :With
$index = 0filter_varreturns the filtered value ; usingFILTER_VALIDATE_INT, I suppose you are filtering to get an integer — and 0 is integer.Which means your call to
filter_varwill return0.0is considered asfalse(see Converting to boolean) — so, you will enter into theifblock ; and the exception will be thrown.Considering
filter_varreturns :falsewhen the filter failed,0is a valid data that can be returned,You should probably use the
===operator (see Comparison Operators), to compare the returned value tofalse. Which means some code that would look like this :