Jump to content


Photo

Endless recurtion


  • Please log in to reply
3 replies to this topic

#1 eric1235711

eric1235711
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts
  • LocationSão Paulo - Brazil

Posted 07 April 2006 - 12:47 PM

Hello,

I´m doing some tests...

and look this one:

<?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);
?>

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

I just ran this one:

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

}
echo test2(0);


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?


Programming is like building your own universe...
But let me ask you something:
Your world... It´s cool? Or it sucks?

#2 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 07 April 2006 - 01:07 PM

[!--quoteo(post=362496:date=Apr 7 2006, 08:47 AM:name=eric1235711)--][div class=\'quotetop\']QUOTE(eric1235711 @ Apr 7 2006, 08:47 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hello,

I´m doing some tests...

and look this one:

<?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);
?>

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.

<?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

?>



#3 eric1235711

eric1235711
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts
  • LocationSão Paulo - Brazil

Posted 07 April 2006 - 04:10 PM

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?
Programming is like building your own universe...
But let me ask you something:
Your world... It´s cool? Or it sucks?

#4 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 07 April 2006 - 05:26 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users