views:

75

answers:

3

I have a function that calls two other functions:

class myClass{


    function myFunc(){

        for($i=0;$i<500;$i++){
            $this->func1();
            $this->func2();
        }
    }

    function func1(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true)
             echo "This is from func1, and may echo 0-1000 times";
    }

    function func2(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true)
             echo "This is from func2, and may echo 0-1000 times";
    }
}

What I'd like to do is figure out a way that I can get the total times the functions have echo'd something and get that info to display in myFunc(). I wrote a count function, but it didn't work out the way I had expected.

Any suggestions?

+4  A: 

Here's one way:

class myClass{
    private $count;

    function myFunc(){

        for($i=0;$i<500;$i++){
            $this->func1();
            $this->func2();
        }
    }

    function func1(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true) {
             $this->count++;
             echo "This is from func1, and may echo 0-1000 times";
         }
    }

    function func2(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true) {
             $this->count++;
             echo "This is from func2, and may echo 0-1000 times";
         }
    }
}

or a better way:

class myClass{
    private $count;

    function myFunc(){

        for($i=0;$i<500;$i++){
            $this->func1();
            $this->func2();
        }
    }

    function func1(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true) {
             echoMe("This is from func1, and may echo 0-1000 times");
         }
    }

    function func2(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true) {
             echoMe("This is from func2, and may echo 0-1000 times");
         }
    }

    function echoMe($msg) {
        echo $msg;
        $this->count++;
    }
}
Zack
You have to remove `$this->echo++` from the two functions.
Felix Kling
thanks, that's what I was needing!
kylex
Felix: Thanks, I updated the second example.
Zack
A: 

Why not have the functions return the echo count:

$echoCount = 0;
while ($something == true) {
    echo "This is from func1, and may echo 0-1000 times";
    $echoCount++;
}

return $echoCount;

Then in myFunc, you can accumulate them:

function myFunc() {

    $totalEchoes = 0;
    for ($i=0; $i<500; $i++) {
        $totalEchoes += $this->func1() + $this->func2();
    }
}
webbiedave
+1  A: 
function myFunc(){
    $echo_count = 0;
    for($i=0;$i<500;$i++){
        $echo_count += $this->func1();
        $echo_count += $this->func2();
    }
    echo $echo_count;
}

function func1(){
     // Does some stuff
     // echos statements, and can vary the amount of echoed statements
     $count = 0;
     while($something == true){
         echo "This is from func1, and may echo 0-1000 times";
         $count++;
     }
     return $count;
}

function func2(){
     // Does some stuff
     // echos statements, and can vary the amount of echoed statements
     $count = 0;
     while($something == true){
         echo "This is from func2, and may echo 0-1000 times";
         $count++;
     }
     return $count;
}
john