Jump to content

Endless recurtion


eric1235711

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?

Link to comment
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]

Link to comment
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?
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.