Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-02-2011, 09:06 PM   PM User | #1
Mac II
New to the CF scene

 
Join Date: Feb 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Mac II is an unknown quantity at this point
OOP Calculator [Mac] (What you think about that code ?)

Please , just tell me if it is functionality , if not give new source and i will study it , but if it is don't give new source because it is still functionality .

PHP Code:
<?php
class Calculator {
    public 
$x;
    public 
$y;
    public 
$operator;
    public function 
calculate($x$operator$y) {
        
$this->setX($x);
        
$this->setY($y);
        
$this->setOperator($operator);
        if(!isset(
$this->x) or !isset($this->operator) or !isset($this->y) or !is_numeric($this->x) or !is_numeric($this->y)):
            throw new 
exception('X, operator or Y is not set or X or Y is not numeric');
        else:
            if(
$this->operator !== "+" or $this->operator !== "-" or $this->operator !== "*" or $this->operator !== "/"):
                throw new 
invalidargumentexception('Invalid Operator');
            else:
                switch(
$this->operator):
                    case 
"+":
                        echo 
$x $y;
                    break;
                    case 
"-":
                        echo 
$x $y
                    break;
                    case 
"*":
                        echo 
$x $y;
                    break;
                    case 
"/":
                        echo 
$x $y;
                    break;
                endswitch;
            endif;
        endif;
    }
    public function 
setX($string) {
        
$this->$string;
    }
    public function 
setY($string) {
        
$this->$string;
    }
    public function 
setOperator($string) {
        
$this->operator $string;
    }
}
$obj = new Calculator;
$obj->calculate("1""+""1");
?>
Thanks!

Last edited by Mac II; 03-02-2011 at 09:11 PM..
Mac II is offline   Reply With Quote
Old 03-03-2011, 12:33 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You don't need any members for this at all since thing will need to persist. The 'x', 'y', and 'operator' setters are redundant since they are overwritten with any calculate call.
When doing OO code though, you may be better off using a composite model to write things like a calculator. It allows for extremely easy updating of functionality.
PHP Code:
<?php
interface ICalculation
{
    public function 
calculate($lhs$rhs);
}

class 
Calculator
{
    public static function 
calculate($lhs$rhsICalculation $fp)
    {
        return 
$fp->calculate($lhs$rhs);
    }
}

class 
CalculationAdd implements ICalculation
{
    public function 
calculate($lhs$rhs)
    {
        if (!
is_numeric($lhs) || !is_numeric($rhs))
        {
            throw new 
Exception(NAN);
        }
        return 
$lhs $rhs;
    }
}

class 
CalculationSubtract implements ICalculation
{
    public function 
calculate($lhs$rhs)
    {
        if (!
is_numeric($lhs) || !is_numeric($rhs))
        {
            throw new 
Exception(NAN);
        }
        return 
$lhs $rhs;
    }
}

class 
CalculationStringConcat implements ICalculation
{
    public function 
calculate($lhs$rhs)
    {
        return 
$lhs $rhs;
    }
}

// Usage
$a 10;
$b 5;

printf('$a + $b = %f' PHP_EOLCalculator::calculate($a$b, new CalculationAdd()));
printf('$a - $b = %f' PHP_EOLCalculator::calculate($a$b, new CalculationSubtract()));
try
{
    
printf('"cat" - "dog" = %f' PHP_EOLCalculator::calculate('cat''dog', new CalculationAdd()));
}
catch (
Exception $ex)
{
    print 
$ex->getMessage() . PHP_EOL;
}

printf('"cat" - "dog" = %s' PHP_EOLCalculator::calculate('cat''dog', new CalculationStringConcat()));
Validation is much much easier this way since its done at a per calculation basis. 'cat' + 'dog' using CalculateAdd will fail with a NAN, but 'cat' + 'dog' using a CalculateStringConcat will concat together. There is no end to the calculations you can do this way, but it is bound by a $lhs and $rhs only rule (which is fine anyway). All that needs to happen is a new class written to perform the calculation.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:38 AM.


Advertisement
Log in to turn off these ads.