{ private $result = 1; //private $number; function __construct($number) { $this->number = $number; for ($i = 2; $i <= $number; $i++) { $this->result *= $i; } } public function showResult() { echo "Factorial of {$this->number} is {$this->result}. "; } } $fact = new Factorial(8); $fact->showResult(); //class.statictester.php class StaticTester { private static $id = 0; function __construct() { self::$id += 1; } public static function checkIdFromStaticMehod() { echo "Current Id From Static Method is " . self::$id . "\n"; } public function checkIdFromNonStaticMethod() {
public function execute($operatorName) { if ($operatorName == "+") { $addobject = new Add(); $addobject->execute($this->values); //$this->add(); } else { if ($operatorName == "-") { $subtractobject = new Subtract(); $subtractobject->execute($this->values); //$this->subtract (); } else { if ($operatorName == "!") { $factorialobject = new Factorial(); $factorialobject->execute($this->values); //$this->factorial (); } else { try { throw new NoSuchOperator('No such operator!'); } catch (NoSuchOperator $e) { echo 'Caught exception: ' . $e->getMessage(); } } } } }
<?php class Factorial { private $input; public function __construct($input) { $this->input = $input; } public function execute() { $answer = 1; $x = 1; while ($x <= $this->input) { $answer = $answer * $x; $x = $x + 1; } return $answer; } } $f = new Factorial(10); $result = $f->execute();
public function testFactorialOfNumberTwenty() { $factorial = new Factorial(20); $this->assertEquals(2.43290200817664E+18, $factorial->getResult()); }
include_once 'class.factorial.php'; include_once 'class.wordcounter.php'; include_once 'class.htmlemailer.php'; include_once 'class.dbmanager.php'; include_once 'class.statictester.php'; include_once 'class.student.php'; include_once 'class.overloader.php'; /*---------- Extended Emailer ----------*/ $emailerobject = new ExtendedEmailer(); $emailerobject->setSender('*****@*****.**'); $emailerobject->addRecipients('*****@*****.**'); $emailerobject->setSubject('Just a Test'); $emailerobject->setBody('Content of Email'); $emailerobject->sendEmail(); /*---------- Factorial ----------*/ $factorial = new Factorial(5); $factorial->showResult(); /*---------- WordCounter ----------*/ $count = new WordCounter('words.txt'); $count->count(WordCounter::ASC); /*---------- HTML Emailer ----------*/ $newemailerobject = new HtmlEmailer('wdarking'); $newemailerobject->addRecipients('*****@*****.**'); $newemailerobject->setSubject('*****@*****.**'); $newemailerobject->setBody('Hi there :D'); $newemailerobject->sendEmail(); $newemailerobject->sendHtmlEmail(); /*---------- DBDriver ----------*/ $dbdriver = DBManager::getMySQLDriver(); /*---------- Static Tester ----------*/ $tester = new StaticTester();