开发者

PHP Object constant runtime Maximum function nesting level

开发者 https://www.devze.com 2023-04-10 00:51 出处:网络
I\'m reaching the maximum function nesting leve开发者_如何学编程l (full list at the end of question). I realize that the solution to this is xdebug.max_nesting_level, but what are the detriments to th

I'm reaching the maximum function nesting leve开发者_如何学编程l (full list at the end of question). I realize that the solution to this is xdebug.max_nesting_level, but what are the detriments to that? Also, how can I better implement my code.

I'm writing an irc client that right now calls itself a lot.

Call Stack (folded)

#     |    Time        |    Memory     |    Function           |    Location
1     |    0.0010      |    800152     |    {main}( )          |    ..\index.php:0
2     |    0.0010      |    802416     |    IRCBot->__construc |    ..\index.php:225
3     |    0.1104      |    804368     |    IRCBot->cont( )    |    ..\index.php:34
4     |    0.1945      |    814592     |    IRCBot->cont( )    |    ..\index.php:144
......|................|...............|.......................|.....................
96    |    113.8191    |    1121560    |    IRCBot->cont( )    |    ..\index.php:144
97    |    114.0116    |    1126928    |    IRCBot->cont( )    |    ..\index.php:144
98    |    114.2020    |    1132384    |    out( )             |    ..\index.php:105
99    |    114.2020    |    1132384    |    flush2( )          |    ..\index.php:14

I know I can solve this by increasing the max_nesting_level, but then what happens when the nesting level gets to the new max? Also, is the way that I'm doing this bad for memory etc.

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

Questions:

Is increasing the max_nesting_level going to increase load on my server?

Is there any way to re-engineer this code to avoid this issue?

Is it bad to run PHP scripts like this on a CGI installation?


Sure a while loop would be more efficient. And I also believe you actually want to store all the date that comes back from the socket and not overwrite the contents of $data each time. Additionally it seems not useful to pass the $config variable here. Here an updated version:

function cont() {
    while (!feof($this->socket)) {
        fgets($this->socket, 256);
    }
}


Recursion is expensive from both a memory and computation standpoint, and if you're already going past 100 calls, that should alert you to the fact that this is not the right application of a recursive call.

For getting data from a socket it absolutely is not a problem that should be solved using recursion.

0

精彩评论

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