NYCPHP Meetup

NYPHP.org

[nycphp-talk] multiple classes writing to a common container

Chris Bielanski Cbielanski at inta.org
Fri Mar 19 14:26:22 EST 2004


I wonder if it would be possible for the classes to receive a handle to a
debug object (since you have a debug class) then each class on which you'd
want to report merely calls a method on it's "localized" debugger.
Instantiate a single debug object, hand its reference to the constructor in
each class, set it "on" or "off" and let the debug object itself control how
and when it dumps to the screen.

On paper, it sounds like it should play nicely. Please apprise me of the
situation if it doesn't. I'd love to know how it turns out.

~Chris

> I spent a bunch of time spinning around on this, until I 
> changed how I 
> was passing the reference of the collector to the working classes.
> 
> Here's the (working) code:
> <?php
> 
> /**
> * various tests
> */
> 
> class Collector
> {
>     var $collection;
>    
>     function addItem($item)
>     {
>         $this->collection[] = $item;
>         return TRUE;
>     }
>    
>     function getCollection()
>     {
>         echo "<pre>";
>         print_r($this->collection);
>         echo "</pre>";
>         //return TRUE;
>     }
> }
> 
> class testA
> {
>     /*
>     * this class takes three numbers and checks to see
>     * if the first two add up to the last one
>     */
>     var $collection;
>    
>     function testA($a)
>     {
>        
>         /* $a is the reference to the Collector object */
>         $this->collection =& $a;
>     }
>    
>     function test($a, $b, $c)
>     {
>         if ($c == ($a + $b))
>         {
>             echo " testA is true<br>";
>             return TRUE;
>         }else{
>             echo " testA is false<br>";
>             $this->error($a ." + ". $b ." != ". $c);
>             return FALSE;
>         }
>     }
> 
>     function error($text)
>     {
>         /* adds the text to the common collection */
>         $tmp = $this->collection->addItem($text);
>         return TRUE;
>     }
> }
> 
> class testB
> {
>     /*
>     * this class takes three numbers and checks to see
>     * if subtracting the first from the second equals
>     * the last one
>     */
>     var $collection;
> 
>     function testB($a)
>     {
>         /* $a is the reference to the Collector object */
>         $this->collection =& $a;
>     }
> 
>     function test($a, $b, $c)
>     {
>         if ($c == ($a - $b))
>         {
>             echo " testB is true<br>";
>             return TRUE;
>         }else{
>             echo " testB is false<br>";
>             $this->error($a ." - ". $b ." != ". $c);
>             return FALSE;
>         }
>     }
> 
>     function error($text)
>     {
>         /* adds the text to the common collection */
>         $tmp = $this->collection->addItem($text);
>         return TRUE;
>     }
> }
> 
> 
> $coll = new Collector();  // create a new collection
> $testA = new testA(&$coll);  // pass the reference to the 
> common collection
> $testB = new testB(&$coll);  // pass the same collection reference to 
> this class
> 
> /* create an array of test data */
> $tests = array (
> 0 => array(1, 1, 2), 1 => array (1, 2, 3), 2 => array(1, 3, 4),
> 3 => array(1, 3, 2), 4 => array (1, 4, 3), 5 => array(7, 3, 4)
> );
> 
> $count = count($tests);
> 
> $msg = 0;
> 
> /* run through the tests */
> for ($x=0; $x<$count; $x++)
> {
>     if (!$testA->test($tests[$x][0],$tests[$x][1],$tests[$x][2])) // 
> addition test
>     {
>         $msg++;  // if the test fails then increment the flag, so we 
> know to display the collection
>     }
>     if (!$testB->test($tests[$x][0],$tests[$x][1],$tests[$x][2])) // 
> subtraction test
>     {
>         $msg++; // if the test fails then increment the flag, 
> so we know 
> to display the collection
>     }
> }
> 
> if ($msg > 0)
> {
>     echo $coll->getCollection();
> }
> 
> ?>
> 
> This code outputs:
> testA is true
> testB is false
> testA is true
> testB is false
> testA is true
> testB is false
> testA is false
> testB is false
> testA is false
> testB is false
> testA is false
> testB is true
> 
> Array
> (
>     [0] => 1 - 1 != 2
>     [1] => 1 - 2 != 3
>     [2] => 1 - 3 != 4
>     [3] => 1 + 3 != 2
>     [4] => 1 - 3 != 2
>     [5] => 1 + 4 != 3
>     [6] => 1 - 4 != 3
>     [7] => 7 + 3 != 4
> )
> 
> 
> -- 
> Chris Hubbard
> Sr Software Developer
> Next Online
> 425 563 4153
> 
> 
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
> 



More information about the talk mailing list