Saturday, 24 August 2013

Problems with PHP code or a bug

Problems with PHP code or a bug

I am having weird issues with my code. I have almost identical code to the
one below (I have not provided the actual code b`cuz it is a little big
library with a lot of dynamic generation(selecting classes based on path
routing etc. i.e. framework)).
The code explained:
ClassA represents the current route object. Contains controller, route
string etc.
ScriptAClassAction is the dispatcher, checking if the route has everything
that is necessary for the execution and running everything, does the
controller exists $!empty(reflection) and does the action exist in the
controller $reflection->hasMethod('hello').
In my world the parent if should be triggered if both conditions are meant
(and the are not) or the else should get triggered, which is checking to
see which of the checks have failed. On execution time I see that the
first check passes (which I think it is a bug in PHP) and after it the
else gets triggered, and then the second if.
I am thinking that this may be a bug in PHP, but I doubt it a lot. Does
anyone see something that I miss @ 1:50AM ?
PHP 5.3.27 with enabled xDebug(No other extensions) & Apache 2.2.25 (I
believe Apache is irrelevant here but..), Windows 7 x86 Home Premium
// ClassA.php
class A {
public function __construct(){print "Init";}
public function bye(){print "Hey :)";}
}
//ScriptAClassAction.php
require 'ClassA.php';
$class = new A();
$reflection = new ReflectionClass($class);
if (!empty($reflection) && $reflection->hasMethod('hello')){
$class->hello(); // False positive trigger
} else {
if (!$reflection) // I know this might not be the best check but..
print "Not a valid class supplied";
if (false == $reflection->hasMethod('hello')) // True trigger
print "Supplied class does not have any manners and does not greet
you :D";
}

No comments:

Post a Comment