-4

他の 2 つの関数を呼び出す関数があります。

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";
    }
}

私がやりたいのは、関数が何かをエコーし​​た合計回数を取得し、その情報を myFunc() に表示する方法を見つけることです。カウント関数を書いたのですが、思ったように動きませんでした。

助言がありますか?

4

3 に答える 3

4

これが1つの方法です:

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";
         }
    }
}

またはより良い方法:

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++;
    }
}
于 2010-06-23T20:43:26.617 に答える
1
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;
}
于 2010-06-23T20:45:36.510 に答える
0

関数にエコーカウントを返さないのはなぜですか。

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

return $echoCount;

次に、myFuncで、それらを蓄積できます。

function myFunc() {

    $totalEchoes = 0;
    for ($i=0; $i<500; $i++) {
        $totalEchoes += $this->func1() + $this->func2();
    }
}
于 2010-06-23T20:44:10.737 に答える