PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Recursion... Sort of...
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Recursion... Sort of...

Réponse
 
LinkBack Outils de la discussion
Vieux 08/05/2008, 23h48   #1
Matt Neimeyer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Recursion... Sort of...

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
  Réponse avec citation
Vieux 09/05/2008, 01h33   #2
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Recursion... Sort of...

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

  Réponse avec citation
Vieux 09/05/2008, 02h23   #3
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Recursion... Sort of...

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

  Réponse avec citation
Vieux 09/05/2008, 02h28   #4
David Otton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Recursion... Sort of...

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.
  Réponse avec citation
Vieux 09/05/2008, 03h02   #5
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Recursion... Sort of...

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

  Réponse avec citation
Vieux 09/05/2008, 09h52   #6
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Recursion... Sort of...

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/
  Réponse avec citation
Vieux 09/05/2008, 18h03   #7
Matt Neimeyer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Recursion... Sort of...

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
  Réponse avec citation
Vieux 09/05/2008, 19h06   #8
tedd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Recursion... Sort of...

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
  Réponse avec citation
Vieux 11/05/2008, 01h39   #9
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: unsubscribe

bobcray wrote:
> unsubscribe


Go to http://www.php.net/mailing-lists.php and do it yourself!
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 15h42.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16798 seconds with 17 queries