inherited scope in a function (CFPARAM)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Indrid Cold

    #1

    inherited scope in a function (CFPARAM)

    Is there a way to call a function so the function shares the scope of the
    caller?

    For example, I was playing around with making a php version of the cold
    fusion tag CFPARAM

    What that cold fusion tag/sorta-function does is:
    1) Check if a variable exists
    2) If it doesn't, creat it with a default value

    I could do it inline like this:
    eval("if(empty( \$testVar)) \testVar=null;" );

    but I don't see how I could do it as a function, like say
    phParam("test")
    .... is there any way for php to see all variables in the caller's scope
    without passing them in or declaring them all global?
  • Andy Hassall

    #2
    Re: inherited scope in a function (CFPARAM)

    On Sun, 29 Feb 2004 15:44:40 GMT, Indrid Cold <nobody@nowhere .com> wrote:
    [color=blue]
    >Is there a way to call a function so the function shares the scope of the
    >caller?
    >... is there any way for php to see all variables in the caller's scope
    >without passing them in or declaring them all global?[/color]

    Not that I'm aware of.

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

    Comment

    • Bruno Desthuilliers

      #3
      Re: inherited scope in a function (CFPARAM)

      Indrid Cold wrote:[color=blue]
      > Is there a way to call a function so the function shares the scope of the
      > caller?
      >
      > For example, I was playing around with making a php version of the cold
      > fusion tag CFPARAM
      >
      > What that cold fusion tag/sorta-function does is:
      > 1) Check if a variable exists
      > 2) If it doesn't, creat it with a default value
      >
      > I could do it inline like this:
      > eval("if(empty( \$testVar)) \testVar=null;" );
      >
      > but I don't see how I could do it as a function, like say
      > phParam("test")
      > ... is there any way for php to see all variables in the caller's scope
      > without passing them in or declaring them all global?[/color]

      The good news is that you can quite easily solve the problem of passing
      vars as params, with get_defined_var s, compact() and extract() :

      <?php
      $glo = array(1, 1, 2, 3, 5, 8);

      function caller($arg1, $arg2) {
      global $glo;
      $loc = 33;
      $arr = get_defined_var s();

      // show what is defined in the current scope :
      echo "<hr>\nIn caller :<br>\n"
      . nl2br(var_expor t($arr, true)) . "<hr>\n";

      // and pass all context to the callee
      callee(compact( array_keys($arr )));
      }


      function callee($args) {
      extract($args);
      unset($args); // optional...
      // show what is defined in the current scope :
      echo "<hr>\nIn caller :<br>\n"
      . nl2br(var_expor t(get_defined_v ars(), true)) . "<hr>\n";
      }

      caller('meaning oflife', 42);
      ?>

      The bad news is that if you need to share so much state, then your
      design is probably a bit flawed.

      My 2 cents,
      Bruno

      Comment

      Working...