Functions for variables

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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
Does someone know where that article is, or how to do this?  Thanks.

Re: Functions for variables wrote:
Quoted text here. Click to load it

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?


Re: Functions for variables

Oli Filth wrote:
Quoted text here. Click to load it

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

Re: Functions for variables wrote:
Quoted text here. Click to load it

In PHP5 you could do something like the following:


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

$joe = '';
$setVar = new SetVar;
print $joe;


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


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.

Re: Functions for variables said the following on 02/04/2006 20:57:
Quoted text here. Click to load it

Yes, but why?

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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


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:

class Output
   var $main;
   var $menu;

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

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

   function display()
      print "var menu = ;";
      print "var main = ;";
      print "menuUpdate(menu);";
      print "mainUpdate(main);";

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

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


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.

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?

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('main', 'this is some text');

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

Thanks for the help.

Site Timeline