PDA

View Full Version : How to call variable within function?


redfroc
12-01-2007, 05:46 AM
Dear all,

how to call variable from function to another function or global section?

class first_class{
function sayHello(){
print "hello <br />";
$me = "this is me <br />";
}
function sayHelloToo(){
print "hello too <br />";
print "you said: " . $me;
$you = " and i see you";
}
}

$obj = new first_class();

$obj->sayHello();
print $me;

$obj->sayHelloToo();
print $you;

It can't return the values. :(

Thank you.

Inigoesdr
12-01-2007, 06:00 AM
Make the variables members of the class:
class first_class{
var $me = '';
var $you = '';

function sayHello(){
print "hello <br />";
$this->me = "this is me <br />";
}
function sayHelloToo(){
print "hello too <br />";
print "you said: " . $me;
$this->you = " and i see you";
}
}

$obj = new first_class();

$obj->sayHello();
print $obj->me;

$obj->sayHelloToo();
print $obj->you;

redfroc
12-01-2007, 09:45 AM
Oh, thanks. I got it.

One more question, pls.. :D
Can I use 'global' syntax to the variable in order to make it as global variable and can be accessed from everywhere, even nested function?

Would you like to explain me about how doing this to this script, please..?
Thank you very much. :)

Inigoesdr
12-01-2007, 10:04 AM
Oh, thanks. I got it.

One more question, pls.. :D
Can I use 'global' syntax to the variable in order to make it as global variable and can be accessed from everywhere, even nested function?

Would you like to explain me about how doing this to this script, please..?
Thank you very much. :)

No you may not. (yes you can, but I'm not going to tell you how because you shouldn't need to use it)