计算函数回显语句的次数

发布于 2024-09-06 22:51:56 字数 773 浏览 5 评论 0原文

我有一个调用其他两个函数的函数:

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() 中。我写了一个计数函数,但它没有按照我预期的方式工作。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦亿 2024-09-13 22:51:56

这是一种方法:

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

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++;
    }
}
暮光沉寂 2024-09-13 22:51:56
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;
}
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;
}
蓝眼泪 2024-09-13 22:51:56

为什么不让函数返回回显计数:

$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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文