LLLLLLL Posted June 30, 2016 Share Posted June 30, 2016 class ufo { const MP_SIZE_LARGE = 600; const MP_SIZE_MEDIUM = 400; const MP_SIZE_SMALL = 250; const LARGE = 'LARGE'; const MEDIUM = 'MEDIUM'; const SMALL = 'SMALL'; public $mp_size; public function desired_width() { switch ( $this->mp_size ) { case ufo::LARGE : return ufo::MP_SIZE_LARGE; case ufo::MEDIUM : return ufo::MP_SIZE_MEDIUM; case ufo::SMALL : return ufo::MP_SIZE_SMALL; default: return 0; } } } So check out the class and function above. What should "desired width" return if mp_size is 0? It makes no sense that the value is anything but 0. Instead, it returns 600. In fact, it will return 400 if I put the "medium" case first. Why would it do that? The function returns expected values if I put strval() around the mp_size. Can someone explain that? This makes absolutely no sense to me. I can't see how the order of the cases could possibly matter, and I also can't see how any case would be "true" when 0 doesn't equal "LARGE". Quote Link to comment https://forums.phpfreaks.com/topic/301417-function-returns-wrong-value/ Share on other sites More sharing options...
Solution Jacques1 Posted June 30, 2016 Solution Share Posted June 30, 2016 You're trying to compare a number (0) with a string (ufo::LARGE). In a strongly typed language, your code wouldn't even run. In a weakly typed language like PHP, the values get converted. And the integer value of "LARGE" is in fact 0: <?php var_dump((int) 'LARGE'); var_dump(0 == 'LARGE'); If you don't want this to happen, don't compare apples and oranges. I wouldn't even allow invalid values for the $mp_size attribute. Write a proper setter which rejects invalid values. Quote Link to comment https://forums.phpfreaks.com/topic/301417-function-returns-wrong-value/#findComment-1534151 Share on other sites More sharing options...
LLLLLLL Posted June 30, 2016 Author Share Posted June 30, 2016 I'm aware of how strongly typed languages work, and I would write different code. Sometimes weakly-typed languages have advantages. (mp_size has other reasons to be ints) You're saying that ... 0 == (int)"LARGE" ... when I would have thought that "0" != "LARGE" So I guess it's a matter of what gets converted to what, and I was thinking about it the opposite way of what actually occurs. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/301417-function-returns-wrong-value/#findComment-1534152 Share on other sites More sharing options...
Jacques1 Posted June 30, 2016 Share Posted June 30, 2016 PHP type comparison tables Sometimes weakly-typed languages have advantages. (mp_size has other reasons to be ints) The attribute isn't limited to strings and integers, though. It can be set to absolutely anything. While this may be acceptable in small non-professional applications where bugs aren't a problem, I definitely wouldn't use public attributes for anything critical. Quote Link to comment https://forums.phpfreaks.com/topic/301417-function-returns-wrong-value/#findComment-1534153 Share on other sites More sharing options...
LLLLLLL Posted June 30, 2016 Author Share Posted June 30, 2016 Yes yes, fine. Why does every PHPFreaks answer have to be some show of chest-thumping? Gosh, I thanked you for your answer. I'm well aware that "the attribute isn't limited to strings and ints", but hey, if you feel better about yourself with your guru-like knowledge-dropping, then congrats. Again, thank you for your initial response. Quote Link to comment https://forums.phpfreaks.com/topic/301417-function-returns-wrong-value/#findComment-1534154 Share on other sites More sharing options...
Jacques1 Posted June 30, 2016 Share Posted June 30, 2016 Just skip the butthurt diva nonsense, and I'm sure we'll get along just fine. Quote Link to comment https://forums.phpfreaks.com/topic/301417-function-returns-wrong-value/#findComment-1534155 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.