Functions for variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paladin.rithe@gmail.com

    Functions for variables

    I seem to recall seeing an article somewhere about how to easily make
    functions that just set the variables. I have no idea where that was,
    or what to look for.
    I think it defined one function, that set the variable that was named.
    So, if I called joe(40), it would set $joe = 40; jane(60) would set
    $jane=60. And they both would use the same function, template() let's
    say.
    Does someone know where that article is, or how to do this? Thanks.

  • Oli Filth

    #2
    Re: Functions for variables

    paladin.rithe@g mail.com wrote:[color=blue]
    > I seem to recall seeing an article somewhere about how to easily make
    > functions that just set the variables. I have no idea where that was,
    > or what to look for.
    > I think it defined one function, that set the variable that was named.
    > So, if I called joe(40), it would set $joe = 40; jane(60) would set
    > $jane=60. And they both would use the same function, template() let's
    > say.[/color]

    I can't think of any way that you could do this in PHP. There is no
    such thing as "template" functions in PHP.

    Anyway, would you want to do this?

    --
    Oli

    Comment

    • Janwillem Borleffs

      #3
      Re: Functions for variables

      paladin.rithe@g mail.com wrote:[color=blue]
      > I seem to recall seeing an article somewhere about how to easily make
      > functions that just set the variables. I have no idea where that was,
      > or what to look for.
      > I think it defined one function, that set the variable that was named.
      > So, if I called joe(40), it would set $joe = 40; jane(60) would set
      > $jane=60. And they both would use the same function, template() let's
      > say.
      > Does someone know where that article is, or how to do this? Thanks.
      >[/color]

      In PHP5 you could do something like the following:

      <?php

      class SetVar {
      function __call($f, $n) {
      global $$f;
      $$f = $n[0];
      }
      }

      $joe = '';
      $setVar = new SetVar;
      $setVar->joe(1);
      print $joe;

      ?>

      But, as Oli already asked, why would you want to do this?


      JW


      Comment

      • paladin.rithe@gmail.com

        #4
        Re: Functions for variables

        Mainly because I have a bunch of variables that I want to be able to
        set via their respective functions. Mainly, it's an output system
        where each variable is a different id in the html. Via the
        XMLHttpRequest of JavaScript, when these are printed, they will be
        called as JavaScript Code via eval(). While I can write functions that
        take care of all the variables, I thought it would be easier to just
        have one that does the same thing to all of them.

        Comment

        • Chung Leong

          #5
          Re: Functions for variables


          Oli Filth wrote:[color=blue]
          > paladin.rithe@g mail.com wrote:[color=green]
          > > I seem to recall seeing an article somewhere about how to easily make
          > > functions that just set the variables. I have no idea where that was,
          > > or what to look for.
          > > I think it defined one function, that set the variable that was named.
          > > So, if I called joe(40), it would set $joe = 40; jane(60) would set
          > > $jane=60. And they both would use the same function, template() let's
          > > say.[/color]
          >
          > I can't think of any way that you could do this in PHP. There is no
          > such thing as "template" functions in PHP.
          >
          > Anyway, would you want to do this?
          >
          > --
          > Oli[/color]

          Well, there is eval() which can be use to define functions and classes
          dynamically.

          Comment

          • Oli Filth

            #6
            Re: Functions for variables

            paladin.rithe@g mail.com said the following on 02/04/2006 20:57:[color=blue]
            > Mainly because I have a bunch of variables that I want to be able to
            > set via their respective functions.[/color]

            Yes, but why?

            [color=blue]
            > Mainly, it's an output system
            > where each variable is a different id in the html. Via the
            > XMLHttpRequest of JavaScript, when these are printed, they will be
            > called as JavaScript Code via eval().[/color]

            What do you mean "they will be called as Javascript code"? And why does
            eval() come into it?

            [color=blue]
            > While I can write functions that
            > take care of all the variables, I thought it would be easier to just
            > have one that does the same thing to all of them.[/color]

            How is calling foo($val) any more useful than $foo = $val; ?


            I think you will need to provide an example of what you're trying to do...



            --
            Oli

            Comment

            • paladin.rithe@gmail.com

              #7
              Re: Functions for variables

              Like I said, I'm using XMLHttpRequest (XHR) in JavaScript to get the
              web pages, and update it dynamicly. I have multiple div id's that need
              updated at each call, so I needed to find a way to do this. One way
              was to make multiple XHR calls, possibly with multiple XHR objects.
              Another way just uses one, send back JavaScript code, and use eval() in
              JavaScript to run it. Here's a rough idea:

              <?php
              class Output
              {
              var $main;
              var $menu;

              function main($script)
              {
              $main .= $script;
              }

              function menu($script)
              {
              $menu .= $script;
              }

              function display()
              {
              print "var menu = {$menu};";
              print "var main = {$main};";
              print "menuUpdate(men u);";
              print "mainUpdate(mai n);";
              }
              }
              ?>

              So, in the php script, that uses the Output class, you would have
              something like (where out is the Output variable:
              out.menu('<a href="option1.p hp">Option1</a>');
              out.menu('<a href="option2.p hp">Option2</a>');
              out.menu('<a href="option3.p hp">Option3</a>');

              out.main('<p>Th is is paragraph one</p>');
              out.main('<p>Th is is another paragraph</p>');
              out.main('<p>Th e last paragraph</p>');

              out.display();

              Now, what is printed ends up stored in the responseText that is
              returned to the JavaScript. I have 2 JS functions, updateMenu(), and
              updateMain() that update their respective areas when called with the
              html as a parameter.
              Since I have multiple (more than those 2) variables, it would be nice
              to have something where I can define one function, and it updates the
              correct variable. But, if I have to do all of them manually, it's not
              a big deal. They're simple enough. I just thought I'd try to save
              myself a little time.

              Comment

              • william.clarke@gmail.com

                #8
                Re: Functions for variables

                Maybe I'm missing the point here, but why not just use php to
                dynamically generate the page? Why is the use of AJAX/XMLHttpRequest
                key to the development of this site?

                Comment

                • paladin.rithe@gmail.com

                  #9
                  Re: Functions for variables

                  Mainly because it's a personal project, and I wanted to see what XHR
                  could do. For what I'm doing, it works great.
                  I actually started with php though, and it worked fine for that, but I
                  thought I'd see what the fuss was about, and tested a few things with
                  what I could do.
                  I finally decided to go with this:

                  out.update('mai n', 'this is some text');

                  function update($variabl e, $html) // where update is part of the Output
                  class
                  {
                  $this->$variable .= $html;
                  }

                  Thanks for the help.

                  Comment

                  Working...