Equivalent of upvar

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • am_ggh@hotmail.com

    Equivalent of upvar

    Is there a PHP equivalent of TCL's "upvar" ?

    I will appreciate any insights.
    Andy

  • Oli Filth

    #2
    Re: Equivalent of upvar

    am_ggh@hotmail. com wrote:[color=blue]
    > Is there a PHP equivalent of TCL's "upvar" ?
    >[/color]

    Don't know TCL, but having found out what upvar is, I guess "global" is
    the closest paradigm in PHP, see
    http://www.php.net/manual/language.v...s.scope.global.

    --
    Oli

    Comment

    • Erwin Moller

      #3
      Re: Equivalent of upvar

      am_ggh@hotmail. com wrote:
      [color=blue]
      > Is there a PHP equivalent of TCL's "upvar" ?
      >
      > I will appreciate any insights.
      > Andy[/color]

      Hi Andy,

      upvar?
      [if memory serves me well, TCL long time ago]
      Isn't that a function to change variables in the environment of the CALLING
      function?
      ------------------
      $a = "hello";
      something();
      echo $a;

      function something() {
      // phantasycode
      upvar ($a, "Joe");
      }
      ------------------
      That outputs Joe, right?
      upvar IN function something() accesses variable $a in the
      context/environment of the place where something() is called.
      (That always confused the hell out of me.)

      If that is what upvar does, I do not think there exists a PHP equivalent.
      (That is: I never saw it)

      But if you use objects, you can easily achieve the same functionality, I
      expect.
      Maybe using $_GLOBAL can also help for desired functionality.

      Just my 2 cents...
      (Me no expert on upvar. )

      Regards,
      Erwin Moller

      Comment

      • am_ggh@hotmail.com

        #4
        Re: Equivalent of upvar

        Erwin

        Thanks for replying. I appreciate your input.

        Yes you are right, I have the need to set a value to the variable in
        the calling environment (my case happens to be a function within a
        function), In fact I need to possibly initialize variables (since they
        might not even exist) in the calling function. $GLOBALS etc. lead to
        unwanted side effects because of the nature of the task at hand*.

        :(

        Andy

        P.S. *I realize this goes against the lean clean design theme, but the
        task is one of conversion from one language to another :(

        Comment

        • Volker Hetzer

          #5
          Re: Equivalent of upvar

          am_ggh@hotmail. com wrote:[color=blue]
          > Erwin
          >
          > Thanks for replying. I appreciate your input.
          >
          > Yes you are right, I have the need to set a value to the variable in
          > the calling environment (my case happens to be a function within a
          > function), In fact I need to possibly initialize variables (since they
          > might not even exist) in the calling function. $GLOBALS etc. lead to
          > unwanted side effects because of the nature of the task at hand*.
          >
          > :(
          >
          > Andy
          >
          > P.S. *I realize this goes against the lean clean design theme, but the
          > task is one of conversion from one language to another :([/color]
          Well, maybe then you sould read a book about compiler construction?
          As far as I learned, there's no requirement for reaching into the callers
          frame. But I do admit, a certain swiss guys books make heavy use of that...

          Lots of Greetings!
          Volker

          Comment

          • am_ggh@hotmail.com

            #6
            Re: Equivalent of upvar

            Volker

            I have taken enough compiler construction courses, albeit a long time
            ago. If you have recently read a book are all excited about your new
            knowledge - I congratulate you. I know of at least 2 languages that do
            support it, if the languages only simulate/use some trick at a higher
            level, I'm absolutely fine with it.

            Just so you know I am neither a fan of such practices, not propagating
            it (practices like reaching out to the caller's space etc.). But I am
            converting code from one language, where such a thing is heavily used,
            which is where my question comes from.

            Life is not always so cut and dry, there are always shades of grey in
            between your black and someone's white :)

            I want to thank you for writing, but I'm afraid your advice is
            absolutely useless.

            Andy.

            P.S. Why you are even handling PHP related questions, when you have
            nothing to contribute, is beyond me altogether, but I thank you for
            your time.

            Comment

            • Volker Hetzer

              #7
              Re: Equivalent of upvar

              am_ggh@hotmail. com wrote:[color=blue]
              > Volker
              >
              > I have taken enough compiler construction courses, albeit a long time
              > ago. If you have recently read a book are all excited about your new
              > knowledge - I congratulate you.[/color]
              I studied it.

              [color=blue]
              > P.S. Why you are even handling PHP related questions, when you have
              > nothing to contribute, is beyond me altogether[/color]
              Because call stacks are not an exlusive PHP issue. My contribution
              was to help you avoid the issue.

              [color=blue]
              > but I thank you for your time.[/color]
              Whatever.

              Volker

              Comment

              • Oli Filth

                #8
                Re: Equivalent of upvar

                am_ggh@hotmail. com said the following on 15/09/2005 17:48:[color=blue]
                > Erwin
                >
                > Thanks for replying. I appreciate your input.
                >
                > Yes you are right, I have the need to set a value to the variable in
                > the calling environment (my case happens to be a function within a
                > function), In fact I need to possibly initialize variables (since they
                > might not even exist) in the calling function. $GLOBALS etc. lead to
                > unwanted side effects because of the nature of the task at hand*.
                >[/color]

                Would passing a variable by reference do what you want?

                e.g.

                function foo()
                {
                $a = 5;
                bar($a);
                // now $a == 8
                }

                function bar(&$var)
                {
                // $var is a reference to $a in the callee...
                $var = 8;
                }

                --
                Oli

                Comment

                Working...