OOP PHP II: Arithmetic Operators and Communicating Objects

oop2Object Communication

Hope to get up the final CMS post by tomorrow (July 9), but in the meantime, I wanted to get this next OOP PHP post up. One of the essential elements of OOP in any language is that it is written as communication between objects. Design patterns are simply ways to help arrange that communication process so that things don’t get tangled up. Sequential and procedural programming are never far from the abyss of tangle, and as long as the scripts or code blocks are short, you can get away with it. But for both the sequential and procedural programmers, spaghetti code is lurking like a vulture on the programmer’s shoulder. So in learning OOP, one of the very first things to understand and use is how objects communicate with one another.

In learning PHP or any other language, one of the first things to understand is how to use the arithmetic operators. The six such operators in PHP are – (negation) + (add), – subtract, * (multiply), / (divide) and % (modulus). Usually the next step is to learn about precedence and how to use parentheses to re-order precedence. However, other than a quick mention that modulus refers to finding the remainder in division, there’s usually not a lot of discussion about it. However, for programmers there’s a huge number of uses of modulus. Check out some articles on modular arithmetic. (You’ll never have problems working with 12-hour clocks again using $value % 12.)

Some examples

I put together some simple code examples of using object communication and math operators. It might help to have them handy when viewing the video. Each listing below should be saved as a separate file with the .php extension.

Call to single method object

Client.php

<?php
//Client.php
ini_set("display_errors","1");
ERROR_REPORTING( E_ALL | E_STRICT );
include_once('EZMath.php');
 
class Client
{
    public function __construct()
    {
       $math = new EZMath();
       echo $math->doAdd(7, 22);    
    }  
}
$worker = new Client();
?>

EZMath.php

<?php
//EZMath.php
ini_set("display_errors","1");
ERROR_REPORTING( E_ALL | E_STRICT );
 
class EZMath
{ 
   public function doAdd($alpha,$beta)
   {
        return $alpha + $beta;
   }  
}
?>

Call to multiple method object

Client1.php

<?php
//Client1.php
ini_set("display_errors","1");
ERROR_REPORTING( E_ALL | E_STRICT );
include_once('EZMath1.php');
 
class Client1
{
    public function __construct()
    {
       $cr = "<br />";
       //Enter your own birth year
       $bday = 1985;
 
       $math = new EZMath1();
       echo $math->doNegate(44) . $cr;
       echo $math->doAdd(12, 26) . $cr;
       echo $math->doSubtract(2013, $bday) . " years old $cr";
       echo $math->doMultiply(12, 26) . $cr;
       echo $math->doDivide(500, 25) . $cr;
       echo $math->doModulus(14, 12) . " pm o'clock $cr";  
    }
 
}
 
$worker = new Client1();
 
?>

EZMath1.php

<?php
//EZMath1.php
ini_set("display_errors","1");
ERROR_REPORTING( E_ALL | E_STRICT );
 
class EZMath1
{
 
   public function doNegate($alpha)
   {
        $alpha = -$alpha;
        return $alpha;
   }
 
   public function doAdd($alpha,$beta)
   {
        return $alpha + $beta;
   }
 
   public function doSubtract($alpha,$beta)
   {
        return $alpha - $beta;
   }
 
   public function doMultiply($alpha,$beta)
   {
        return $alpha * $beta;
   }
 
   public function doDivide($alpha,$beta)
   {
        return $alpha / $beta;
   }
 
   public function doModulus($alpha,$beta)
   {
        return $alpha % $beta;
   }
}
 
?>

Coin Flip Game Using Modular Arithmetic

CoinClient.php

<?php
//CoinClient.php
ini_set("display_errors","1");
ERROR_REPORTING( E_ALL | E_STRICT );
include_once('CoinFlip.php');
 
class CoinClient
{
    public function __construct()
    {
       //Instantiates instance
       $flipper = new CoinFlip();
       //Makes a request
       echo $flipper->doFlip();   
    }   
}
$worker = new CoinClient();
?>

CoinFlip.php

<?php
//CoinFlip.php
class CoinFlip
{
   public function doFlip()
   {
    //rand is built-in PHP method for generating random number
    $coin = rand(1,200);
    //Uses modulo operator (%) to generate 1 or 0
    //When you divide by 2 the remainder is always 1 or 0
    //tests for true (1) or false (0)
    $results = $coin % 2;
    if($results)
    {
        $flip="Heads";
    }
    else
    {
        $flip = "Tails";   
    }
    return $flip;
   }
}
 
?>

You can play around with these examples, and you should get the fundamental idea of objects communicating with one another. See if you can create some of your own.

Share

Copyright © 2013 William Sanders. All Rights Reserved.

0 Responses to “OOP PHP II: Arithmetic Operators and Communicating Objects”


  • No Comments

Leave a Reply