Jump to content

Archived

This topic is now archived and is closed to further replies.

eric1235711

Endless recurtion

Recommended Posts

Hello,

I´m doing some tests...

and look this one:

[code]
<?php
class Test{
    var $vari;
    var $subclass;
    // constructor
    function Test($qt){
        $this->vari = "$qt";
        echo $qt.' ';
    $qt ++;
    $this->subclass = new Test($qt);
    }
}

$qt = 0;
$objeto = new Teste($qt);
?>
[/code]

Here it ran 872 levels and php just stopped, returning no errors... do you know what happened?

I just ran this one:

[code]
function test2($qt){
    echo "$qt ";
    flush();
    if($qtd < 10000)
        return test2(++$qtd);
    else
        return 'end';

}
echo test2(0);
[/code]


and the error was the same: 872 levels of recursion and no error message...



I´m asking this because I'm going to do a work that uses recursion. Do you find that it´s better to use another language?

Don´t you find that php should run them while execution time limit haven´t been reached?

Share this post


Link to post
Share on other sites
[!--quoteo(post=362496:date=Apr 7 2006, 08:47 AM:name=eric1235711)--][div class=\'quotetop\']QUOTE(eric1235711 @ Apr 7 2006, 08:47 AM) [snapback]362496[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hello,

I´m doing some tests...

and look this one:

[code]
<?php
class Test{
    var $vari;
    var $subclass;
    // constructor
    function Test($qt){
        $this->vari = "$qt";
        echo $qt.' ';
    $qt ++;
    $this->subclass = new Test($qt);
    }
}

$qt = 0;
$objeto = new Teste($qt);
?>
[/code]

and the error was the same: 872 levels of recursion and no error message...
I´m asking this because I'm going to do a work that uses recursion. Do you find that it´s better to use another language?

Don´t you find that php should run them while execution time limit haven´t been reached?
[/quote]

The problem is "Test" is the class. A function within "Test" called "Test" is now the constructor.

If the constructor creates another class of (itself) like you do. It recursivly keeps calling itself again and again.

[code]
<?php
class Test{

    var $vari;
    var $subclass;

    // constructor
    function Test($qt){
               // constructor (remember, GenSubClass will call this too (in it's own memory space)
                $subclass = NULL; // initialize
        $this->vari = "$qt";
        echo $qt.' ';
    $qt ++;
    }

        function GenSubClass(){ // break into it's own function
          if(!$subclass)$this->subclass = new Test($this->vari);
       }

}

$qt = 0;
$objeto = new Teste($qt);
$objeto->GenSubclass(); // make call seperatly to avoid recurision

?>
[/code]

Share this post


Link to post
Share on other sites
I´m testing the capacity of php with recursive functions and objects...

I made them endless to know the limits.


My doubts are:

1. 872 levels is the limit of php or of my memory?

2. Is there any settings to increase this number of levels?

3. Is there any settings to "recurse" til execution time limit is reached?

Share this post


Link to post
Share on other sites
I'm sorry for overlooking your question.
php is a "loadmodule" command in Apache as you know. Thus it's a pre-compiled library. Whatever amount of memory Apache alots for each module is most likely set in the compiler at runtime (when compiling Apache).

Apache is open source however... i'm sure you could adjust this and recompile the code (i don't know if that would break any EULA though).

I'm not sure what the limit restrictions on Apache... but keep in mind that each connection Apache recieves to it causes it to spawn a seperate thead. The memory allocated to each thread has to be governed in such a crash that your trying to produce. In cases like yours, the thread needs to shutdown gracefully and deliver an error message to the end user.

You definatly did a great test to find out what the limit is set at... but i don't think your going to find an easy way of altering that without tampering with the stability of Apache.


Edit: to answer your questions from what i wrote:
1. 872 levels is the limit of php or of my memory?
A: limit of Apache (or whatever server you are using)

2. Is there any settings to increase this number of levels?
A: edit apache source code

3. Is there any settings to "recurse" til execution time limit is reached?
A: I don't know, maybe someone else can reply to this thread and add to this question.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.