Is this even valid? Because it's working.
if(empty($query)) {
exit(View::forge('error', $data));
//View::forge('error', $data) = object
}
According to PHP manual exit should only accept "string" or "int" as parameter.
So I tried:
if(empty($query)) {
return View::forge('error', $data); //will not display
}
return View::forge('default', $data); //displays
But it seems it ignores it and displays the default template, so I tried combining it with exit:
if(empty($query)) {
return View::forge('error', $data); //will not display
exit;
}
return View::forge('default', $data); //displays
But still the same result, what I want to know that is exit($obj) valid in fuelphp? Because it seems it's working.
What I want to do is if query is empty page would display error template instead of default template, any help would be appreciated, thanks!
If the object's result is a string or an integer then it fits the criteria set down in the manual. Whatever is within the exit
brackets is executed as a PHP line; see the example below:
$a = 4;
$b = 17;
exit(print $a * $b);
this will output
68
so, on your question:
exit(View::forge('error', $data));
If the returned result of View::forge
is a string or other printable output then it fits the criteria set out by exit
and is executed accordingly.
What will not work is a non-integer, non-string result such as:
$a[] = "trees";
$b[] = "cats";
exit(array_merge($a,$b));
Gives:
Notice: Array to string conversion on line 4
Due to this "conversion" I expect that if you pass an object to the exit
function then it will probably try to use the __toString()
class method, if available, (but I'm not certain on this yet).
Fuel's View class has an _toString() method which calls render() to render the view template. So the object returns a string when used like that.