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; } } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted January 28, 2011 Share Posted January 28, 2011 func_get_args() returns an array. Quote Link to comment Share on other sites More sharing options...
DimitriDV Posted January 28, 2011 Author Share Posted January 28, 2011 What is the solution? Quote Link to comment 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'); Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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()))); } } Quote Link to comment 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 Quote Link to comment 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; } } Quote Link to comment Share on other sites More sharing options...
DimitriDV Posted January 31, 2011 Author Share Posted January 31, 2011 Thanks! Quote Link to comment 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.