DimitriDV Posted January 28, 2011 Share Posted January 28, 2011 Hi there, I am starting to learn OO PHP programming. After running math.php, it gives an output 'Use only numbers'. Why? I don't understand this. math.php <?php require('MathClass.php'); $c = new MathClass(); echo $c->add(5,10,15,20); ?> MathClass.php <?php class MathClass{ function add(){ $args = func_num_args(); $sum = 0; $i = 0; for ($i ; $i < $args ; $i++){ is_int(func_get_args($i)) ? $sum += func_get_args($i) : die('Use only numbers'); } return $sum; } } ?> Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/ Share on other sites More sharing options...
trq Posted January 28, 2011 Share Posted January 28, 2011 func_get_args() returns an array. Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1166512 Share on other sites More sharing options...
DimitriDV Posted January 28, 2011 Author Share Posted January 28, 2011 What is the solution? Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1166516 Share on other sites More sharing options...
BlueSkyIS Posted January 28, 2011 Share Posted January 28, 2011 because func_get_args($i) is not an integer. is_int(func_get_args($i)) ? $sum += func_get_args($i) : die('Use only numbers'); Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1166550 Share on other sites More sharing options...
l4nc3r Posted January 28, 2011 Share Posted January 28, 2011 You're putting func_get_args into an is_int function to check if it's an int. But it'll never be of type int because, like thorpe said, func_get_args returns an array. Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1166552 Share on other sites More sharing options...
ignace Posted January 28, 2011 Share Posted January 28, 2011 class Math { public function add() { return array_sum(array_filter(array_map('is_int', func_get_args()))); } } (intval() would have worked too) $math = new Math(); echo $math->add(1,2,3,4,5,'hello','world'); // Output: 15 Don't you just love PHP? Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1166584 Share on other sites More sharing options...
DimitriDV Posted January 31, 2011 Author Share Posted January 31, 2011 Fatal error: func_get_args(): Can't be used as a function parameter in MathClass.php on line 6 Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1167733 Share on other sites More sharing options...
ignace Posted January 31, 2011 Share Posted January 31, 2011 class Math { public function add() { return array_sum(array_filter(array_map('intval', $a=func_get_args()))); } } Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1167738 Share on other sites More sharing options...
DimitriDV Posted January 31, 2011 Author Share Posted January 31, 2011 Sorry, I am still receiving the same error Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1167748 Share on other sites More sharing options...
suma237 Posted January 31, 2011 Share Posted January 31, 2011 class MathClass{ function add(){ $numargs = func_num_args(); $args = func_get_args(); //echo $args; $sum = 0;$i = 0; for ($i ; $i < $numargs ; $i++){ if(is_integer($i)) $sum += $args[$i]; //is_int(func_get_args($i)) ? $sum += func_get_args($i) : die('Use only numbers'); } return $sum; } } Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1167751 Share on other sites More sharing options...
DimitriDV Posted January 31, 2011 Author Share Posted January 31, 2011 Thanks! Link to comment https://forums.phpfreaks.com/topic/225954-basic-object-oriented-script-does-not-work/#findComment-1167752 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.