Re: Recursion... Sort of...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, May 8, 2008 at 6:23 PM, Jim Lucas <lists@xxxxxxxxx> wrote:

> Nathan Nobbe wrote:
>
>> On Thu, May 8, 2008 at 3:48 PM, Matt Neimeyer <matt@xxxxxxxxxxxx> wrote:
>>
>>  Is there a way to tell if a function has been called that has resulted
>>> in a call to the same function?
>>>
>>> We have an in-house CRM app that has a function that draws a tabbed
>>> panel on a screen... BUT if there are sub-sub-tabbed panels we want to
>>> invert the tab colors and panel colors...
>>>
>>> So...
>>>
>>> DrawSubTab($Set) - Black on White
>>>  Content
>>>  DrawSubTab($Set) - White on Black
>>>     Content
>>>     DrawSubTab($Set) - Black on White
>>>        Content
>>>     DrawSubTab($Set) - Black on White
>>>        Content
>>>        Etc...
>>>
>>> I suppose I could rewrite EVERY call to the function with a recursion
>>> count like DrawSubTab($Set,$DepthCount+1) but that would be a MASSIVE
>>> commit... whereas if the function can determine itself... All that
>>> said I can't think of a good way to do it without a bastardized global
>>> variable that track "how deep" we are and there is something that
>>> bothers me about that approach... Unless that really is the easiest
>>> way.
>>>
>>
>>
>> you dont need a global, you can have a variable that persists throughout
>> the
>> request local only to the function itself using the static keyword.
>>
>> function doStuff() {
>>  static $callCount;
>>
>>  if(!isset($callCount))
>>   $callCount = 1;
>>  else
>>    $callCount++;
>>
>>  /// do stuff w/ $callCount to potentially handle sub-tabs and stuff
>>
>>  $callCount--;
>> }
>>
>> -nathan
>>
>>
> Look at the way he wants it to work.  Your way would change alternate the
> color each time the function is called.  I think the best/easiest way to
> keep track of depth will be by passing a variable in the function call
> itself.


actually, i didnt supply the part where he does what he wants with the
depth.  i merely provided a way to track it without using a global
variable.  he could easily do something specific depending upon the depth
with what ive shown.

<?php
function doStuff() {
 static $callCount;

 if(!isset($callCount))
  $callCount = 1;
 else
   $callCount++;

 /// do stuff w/ $callCount to potentially handle sub-tabs and stuff
    if($callCount == 2) {
      echo 'white on black';
    } else {
      echo 'black on white';
    }
    echo PHP_EOL;
}

doStuff();
doStuff();
doStuff();
doStuff();
doStuff();
?>

nathan-nobbes-macbook-pro:~ nnobbe$ php testDepth.php
black on white
white on black
black on white
black on white
black on white

o ya and removed the part where the variable is decremented from the
original ;)  (good call there jim) i was thinking the function was going to
be called recursively at first which is why i had it in there and it would
make sense in that case; however since it isnt going to be called
recursively it doesnt.

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux