hadoob024 Posted February 4, 2011 Share Posted February 4, 2011 I'm having a complete brainfart right now and can't seem to remember this. Tried looking it up in the manual, but couldn't find an example. Here's what I have: class myclass { function testA { } function testB { } } So that's my basic setup. I want to define a variable. I then want to set it's value in testA(). I then want to be able to access the value in testB(). Where and how do I define this variable? I thought all I had to do was something like: class myclass { public $tempvar=''; function testA { $tempvar="my new value"; } function testB { echo $tempvar; } } But this says that $tempvar is equal to NULL. What am I forgetting? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/226728-quick-question-about-variable-scope/ Share on other sites More sharing options...
spfoonnewb Posted February 5, 2011 Share Posted February 5, 2011 This should do it: <?php class myclass { public $tempvar=''; public function testA() { $this->tempvar="my new value"; } public function testB() { echo $this->tempvar; } } $class = new myclass; $class->testA(); $class->testB(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/226728-quick-question-about-variable-scope/#findComment-1170099 Share on other sites More sharing options...
hadoob024 Posted February 5, 2011 Author Share Posted February 5, 2011 Oh yeah! Can't believe I forgot. Cool! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/226728-quick-question-about-variable-scope/#findComment-1170311 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.