Darla Posted February 14, 2007 Share Posted February 14, 2007 Hello I have a class structure like this: class test{ function fetchsomevalues() { $testvalue="1"; } } In another class I wish to use the values in the variables from the class test, I include the class and create a new object of it: $fetcobj= new test; $fetcobj->fetchsomevalues(); How can i access the variable $testvalue from here? Darla Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/ Share on other sites More sharing options...
Balmung-San Posted February 14, 2007 Share Posted February 14, 2007 $testvalue disappears after your function call. The variable you have has been declared with scope local to that function, but it dies beyond that. You might want something like this: <?php class test{ public $testvalue; function fetchsomevalues() { $testvalue = 1; } } $fetcobj = new test; //or should it be test()? Is this one of my Java irks coming out again? $fetcobj->fetchsomevalues(); echo $fetcobj->somevalue; //output: 1 ?> It has to be declared outside of the function to have access to it, and it must be public to access it that way. Also, using a function name like fetchsomevalues() to set a variable is a bad naming idea. The name makes the user think that it will return the value back, in which case you'd want to change fetchsomevalues to this: <?php function fetchsomevalues(){ $somevalue = 1; return $somevalue; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184587 Share on other sites More sharing options...
ToonMariner Posted February 14, 2007 Share Posted February 14, 2007 class test{ function fetchsomevalues() { $this->testvalue="1"; } } $fetcobj= new test; $fetcobj->fetchsomevalues(); echo $fetcobj->testvalue; #chocolate fireguard# Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184590 Share on other sites More sharing options...
Darla Posted February 14, 2007 Author Share Posted February 14, 2007 Hei Thanks both of you. The first answer did not return anything? The second one using $this worked Super! Darla Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184595 Share on other sites More sharing options...
obsidian Posted February 14, 2007 Share Posted February 14, 2007 Hei Thanks both of you. The first answer did not return anything? The second one using $this worked Super! Darla Just a note, as you get into PHP5 and start using some of the more detailed OOP principles, you'll have to be careful about just accessing the variables directly. You'll have to use some accessor methods to get at them. It's actually a very good practice to get into either way. This is what ToonMariner is getting at in his example, I believe. Take note of the following: <?php // PHP5 declaration class myObject { private $value = 1; public getMyValue() { return $this->value; } } $obj = new myObject(); // this will work: echo $obj->getMyValue(); // this will not, since the variable is "private": echo $obj->value; ?> Notice that in PHP4, accessing the variables directly (as in the second example above) will work since you can't declare variables with "public" and "private" in PHP4. Hope this makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184604 Share on other sites More sharing options...
Darla Posted February 14, 2007 Author Share Posted February 14, 2007 Hello obsidian Thanks for info. I tried testing the script you gave but received an error: "Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in testphp5.php on line 6" Seems to be an issue with this line: public getMyValue() { Any idea why? Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184696 Share on other sites More sharing options...
Balmung-San Posted February 14, 2007 Share Posted February 14, 2007 Which version of PHP are you running? Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184697 Share on other sites More sharing options...
Darla Posted February 14, 2007 Author Share Posted February 14, 2007 I have set it up to run php5 Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184703 Share on other sites More sharing options...
Balmung-San Posted February 14, 2007 Share Posted February 14, 2007 Try public function getMyValue() It should work the way it is, but better safe then sorry. Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184706 Share on other sites More sharing options...
Darla Posted February 14, 2007 Author Share Posted February 14, 2007 That did the trick, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184714 Share on other sites More sharing options...
obsidian Posted February 14, 2007 Share Posted February 14, 2007 Try public function getMyValue() It should work the way it is, but better safe then sorry. No, that's right. If you have only "public" before the function name, the parser is expecting a variable name, not a function declaration. "public function" is the appropriate way. That's what I get for trying to write up some quick notes Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184829 Share on other sites More sharing options...
Balmung-San Posted February 14, 2007 Share Posted February 14, 2007 You mean my Java tendencies didn't make me be completely wrong this time? Wow, usually they make me completely and horribly wrong. Quote Link to comment https://forums.phpfreaks.com/topic/38478-solved-basic-question-how-to-fetch-variable-from-class/#findComment-184831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.