|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. Thanks in advance! Matt |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Thu, May 8, 2008 at 3:48 PM, Matt Neimeyer <matt@neimeyer.org> 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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Nathan Nobbe wrote:
> On Thu, May 8, 2008 at 3:48 PM, Matt Neimeyer <matt@neimeyer.org> 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. -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
2008/5/8 Matt Neimeyer <matt@neimeyer.org>:
> Is there a way to tell if a function has been called that has resulted > in a call to the same function? debug_backtrace() Can't comment on performance, though. Its an inelegant solution. > 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... Nested? You may be able to do this in pure CSS, without keeping a depth counter at all. > 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 It's an easy change. function DrawSubTable($Set, $DepthCount = 0) { [..] DrawSubTable($Set, $DepthCount + 1); [..] } By providing a default value, all the calls to DrawSubTable() can remain unchanged. You just have to modify its own calls to itself. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Thu, May 8, 2008 at 6:23 PM, Jim Lucas <lists@cmsws.com> wrote:
> Nathan Nobbe wrote: > >> On Thu, May 8, 2008 at 3:48 PM, Matt Neimeyer <matt@neimeyer.org> 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 tobe 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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 9 May 2008, at 02:02, Nathan Nobbe wrote:
> 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; > } No need for the first if, just give the static var a default value... function doStuff() { static $callCount = 0; $callCount++; .... } Much neater. -Stut -- http://stut.net/ |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Wow! Thanks guys! Here's what I ended up doing... To get...
Black on White - 1 White on Black - 2 Black on White - 3 Black on White - 3 White on Black - 2 Black on White - 3 I had to do something like... function doStuff() { static $callCount = 0; $callCount++; if($callCount%2) { echo 'white on black - '.$callCount; } else { echo 'black on white - '.$callCount; } // Stuff that uses the depth count $callCount--; } If I didn't put in the $callCount--; I ended up with something like this... Black on White - 1 White on Black - 2 Black on White - 3 White on Black - 4 Black on White - 5 White on Black - 6 I saw where it was said that "oh he said it wasn't recursive"... Sorry I wasn't clearer. In my mind a "true" recursive function is a function that operates on it's own output like a factorial... Not just a function that is called inside itself. This got me where I needed to be and it is GREATLY appreciated! Matt |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
At 5:48 PM -0400 5/8/08, Matt Neimeyer 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. > >Thanks in advance! > >Matt If the CRM app uses a browser, then it sounds like a problem that could be solved via css -- parent/child relationships. Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
|
|
![]() |
| Outils de la discussion | |
|
|