I have an issue with my IF-statement, apparently.
Inside a symfony2 controller I have the following PHP:
57 foreach($all as $t) {
58 if ( ($end == null) || ($t->getDate()->format('Y-m-d') <= $end->format("Y-m-d")) ) {
59 if ( ($start == null) || ($t->getDate()->format('Y-m-d') >= $start->format("Y-m-d")) ) {
60 $subset[] = $t;
61 }
62 }
63 }
I get this for an error message:
FatalErrorException: Error: Call to undefined function App\Bundle\Controller\ () in
www/App/src/App/Bundle/Controller/TransactionController.php line 59
If I comment the second IF-statement out like this, it runs without errors.
foreach($all as $t) {
if ( ($end == null) || ($t->getDate()->format('Y-m-d') <= $end->format("Y-m-d")) ) {
/*
if ( ($start == null) || ($t->getDate()->format('Y-m-d') >= $start->format("Y-m-d")) ) {
$subset[] = $t;
}
*/
}
}
So somewhere in my if statement it tries to run a sym2 controller? I am lost...
Both $start
and $end
are valid DateTime
objects, so are each $date
on the $t
objects.
If I var_dump the variables just before the second if-condition they print:
start: object(DateTime)#1149 (3) { ["date"]=> string(19) "2014-12-20 16:39:06" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" }
end: object(DateTime)#1150 (3) { ["date"]=> string(19) "2015-01-19 16:39:06" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" }
t->getDate(): object(DateTime)#1046 (3) { ["date"]=> string(19) "2012-12-11 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" }
The error itself looks suspicious, with that whitespace between the namespace and parentheses:
Call to undefined function App\Bundle\Controller\ ()
Normally you'd expect to see something like:
Call to undefined function App\Bundle\Controller\foo_function()
Whatever is going on, PHP is seeing a "function" with a "blank" name. I have encountered weird situations like this before, when I type really fast and happen to press a control character while inserting text. It often happens around a space character, because on my keyboard the control character is next to the space bar.
Reproducing it will be next to impossible. The best thing to do is just delete the line and retype it again, slowly.
If you're hell-bent on diagnosing it, then on GNU/Linux you can "take the cat to the vet":
$ cat file.php
<?php if (1 == 1) echo 'hi';
$ cat -vet file.php
<?php if (1 == ^@1) echo 'hi';
I used my editor to insert a null-byte in the if statement, which doesn't appear in normal inspection but does show when given to "the vet".
So I don't know whether this is something symfony2 specific, but if I split the IF-condition into two different parts and thus do away with the OR operand || it actually works fine.
The puzzling part is that it didn't have a problem with the first IF-statement (which has a nearly identic condition).
if ( $start == null ) {
$subset[] = $t;
}
elseif ( $t->getDate()->format('Y-m-d') >= $start->format("Y-m-d") ) {
$subset[] = $t;
}