470,862 Members | 1,821 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,862 developers. It's quick & easy.

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.

Apr 2 '06 #1
8 1177
pa***********@gmail.com wrote:
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.


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

Apr 2 '06 #2
pa***********@gmail.com wrote:
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.


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
Apr 2 '06 #3
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.

Apr 2 '06 #4

Oli Filth wrote:
pa***********@gmail.com wrote:
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.


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


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

Apr 2 '06 #5
pa***********@gmail.com said the following on 02/04/2006 20:57:
Mainly because I have a bunch of variables that I want to be able to
set via their respective functions.
Yes, but why?

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().
What do you mean "they will be called as Javascript code"? And why does
eval() come into it?

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.


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
Apr 3 '06 #6
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(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:
out.menu('<a href="option1.php">Option1</a>');
out.menu('<a href="option2.php">Option2</a>');
out.menu('<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>');

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.

Apr 3 '06 #7
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?

Apr 3 '06 #8
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
class
{
$this->$variable .= $html;
}

Thanks for the help.

Apr 5 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by will taubin | last post: by
7 posts views Thread by Angelos | last post: by
99 posts views Thread by David MacQuigg | last post: by
27 posts views Thread by Maximus | last post: by
2 posts views Thread by Bryan Olson | last post: by
9 posts views Thread by Pohihihi | last post: by
13 posts views Thread by Simon Dean | last post: by
7 posts views Thread by Immortal Nephi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.