purencool Posted October 2, 2011 Share Posted October 2, 2011 I have an issue that with some testing on an Access class with a method getControlAccess it should only return true or false but as soon as I place a string of any sort into the return my tests always returns true. Any help to point out if it is the way I am testing or the way I have written the method would be great. In the example below it would return true. In one class I have this method class Access{ public function getControlAccess(){ return 'I am stuffed'; } } In the second class I have this class AccessTests extends Access{ public function controlAccessTest(){ $return = "<ul>"; if ($this->getControlAccess() == TRUE){ $return .= "<li>getControlAcess should be true and is returning:<b> ". $this->getControlAccess() ."</b></li>"; } elseif ($this->getControlAccess() == FALSE){ $return .= "<li>getControlAcess should be false and is returning:<b> ". $this->getControlAccess() ."<b></li>"; } else { $return .= "<li>getControlAcess is really stuffed:<b>". $this->getControlAccess() ."<b></li>"; } $return .= "</ul>"; return $return; } public function setAccessTest($set){ return $set; } } Quote Link to comment https://forums.phpfreaks.com/topic/248295-class-test-issue/ Share on other sites More sharing options...
spfoonnewb Posted October 2, 2011 Share Posted October 2, 2011 Any text value you feed as the return will be considered true (there are special cases). You should explicitly define the value to be false, or return nothing at all. See: http://php.net/manual/en/language.types.boolean.php If you want the literal (string) value "TRUE," use quotes, otherwise it's boolean. Quote Link to comment https://forums.phpfreaks.com/topic/248295-class-test-issue/#findComment-1275025 Share on other sites More sharing options...
purencool Posted October 2, 2011 Author Share Posted October 2, 2011 thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/248295-class-test-issue/#findComment-1275026 Share on other sites More sharing options...
purencool Posted October 2, 2011 Author Share Posted October 2, 2011 could i use is_bool($a) in my access class to force a return of true or false? Quote Link to comment https://forums.phpfreaks.com/topic/248295-class-test-issue/#findComment-1275029 Share on other sites More sharing options...
xyph Posted October 2, 2011 Share Posted October 2, 2011 No, use this <?php $varA = TRUE; $varB = 'somestring'; if( $varA == TRUE ) echo '$varA == TRUE<br>'; if( $varB == TRUE ) echo '$varB == TRUE<br>'; if( $varA === TRUE ) echo '$varA === TRUE<br>'; if( $varB === TRUE ) echo '$varB === TRUE'; ?> Output $varA == TRUE $varB == TRUE $varA === TRUE Quote Link to comment https://forums.phpfreaks.com/topic/248295-class-test-issue/#findComment-1275031 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.