mpsn Posted November 20, 2011 Share Posted November 20, 2011 Hi, I want to build a simple calculator class and one of the function is to calculate the factorial, but for some reason I cannot get this to work. Here is class: ========= <?php class Calculator { function factorial($n) { if($n==1) return 1; else return factorial($n-1)*$n; } }//END CLASS Calculator $calculator=new Calculator(); print $calculator->factorial(3); ?> But it doesn't work, I know this is simple but I am a little rusty with object oriented programming. I'd appreciated any help! Link to comment https://forums.phpfreaks.com/topic/251510-help-with-php-classes/ Share on other sites More sharing options...
trq Posted November 20, 2011 Share Posted November 20, 2011 function factorial($n) { if($n==1) return 1; else return $this->factorial($n-1)*$n; } Link to comment https://forums.phpfreaks.com/topic/251510-help-with-php-classes/#findComment-1289853 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.