开发者

IRC Php Bot - Nesting Error

开发者 https://www.devze.com 2023-03-03 10:25 出处:网络
I\'m using SimplePhp IRC BOT, you can see all the code right on the page. The problem I\'m running into is the main function calls it recursively, so after 100 calls, it just errors out and I get th

I'm using SimplePhp IRC BOT, you can see all the code right on the page.

The problem I'm running into is the main function calls it recursively, so after 100 calls, it just errors out and I get this error:

Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\xampp\xampp\htdocs\league\bot.php on line 106

I'll paste some of the main function below:

 function main($config)
    {             
            $data = fgets($this->socket, 256);

            echo n开发者_StackOverflow社区l2br($data);

            flush();

            $this->ex = explode(' ', $data);


            if($this->ex[0] == 'PING')
            {
                    $this->send_data('PONG', $this->ex[1]); //Plays ping-pong with the server to stay connected.
            }

            $this->main($config);
     }

Is there a way to achieve the same functionality without the function calling itself recursively? Is it alright to have this level of nesting and should I just increase the nesting limit xdebug?


Yes — a while() loop.

Unlimited recursion of this variety will not work effectively in PHP, as it doesn't eliminate tail calls. While you can increase the nesting limit, you'll just end up burning more and more memory before eventually crashing.

0

精彩评论

暂无评论...
验证码 换一张
取 消