elnaz1010 Posted February 24, 2010 Share Posted February 24, 2010 hi im very new to php, and i want to access a variable that is outside a class, but i cant here is the code: <?php $name="Test"; class Person{ var $name="Amin"; function getName() { return $name; } } $obj=new Person(); echo $obj->getName(); ?> if i set "return $this" to "$this->name" it will give me "Amin". but what i should do if i want the "Test" value? thanks Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/ Share on other sites More sharing options...
Goldeneye Posted February 24, 2010 Share Posted February 24, 2010 Give your getName() function a parameter like so: <?php function getName($name){ //do stuff here } ?> That's how you would pass an outside variable into a function. (There are other ways, as well). And that way, you can do this: <?php $person_name = 'Mike'; $obj->getName($person_name); ?> Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017231 Share on other sites More sharing options...
trq Posted February 24, 2010 Share Posted February 24, 2010 You need to pass the value into the object first. eg; <?php class Person { private $_name; public function setName($name) { $this->_name = $name; } public function getName() { return $this->_name; } } $name = 'Test'; $p = new Person; $p->setName($name); echo $p->getName(); Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017236 Share on other sites More sharing options...
elnaz1010 Posted February 24, 2010 Author Share Posted February 24, 2010 thank you Goldeneye. but it seems i couldnt express my mean (sorry about that). i was trying to access the outer $name (which has a "Test" value) in the class. so the getName function will return "Test" value. but anyway i found the solution, so i say here, maybe someone come up with my problem. i should use "global" keyword in my class function, so the code will be like: <?php $name="Test"; class Person{ var $name="Amin"; function getName() { global $name; return $name; } } $obj=new Person(); echo $obj->getName(); ?> thanks again regards Edit: also thanks to "thorpe" who replied me while i was typing my second post Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017240 Share on other sites More sharing options...
trq Posted February 24, 2010 Share Posted February 24, 2010 If your going to start using globals you may as well stop using classes. The two are counter productive. Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017242 Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2010 Share Posted February 24, 2010 Please DON'T use the global keyword to bring values into a CLASS. That breaks the general purpose nature of a class definition and you might as well not bother making a class (or a function) out of the code. Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017243 Share on other sites More sharing options...
elnaz1010 Posted February 24, 2010 Author Share Posted February 24, 2010 excuse me, but im a bit confused. if im not supposed to use globals, then what i should do in a situation like this? i like to return "Test" with my getName function. i know my questions is very newbie, please bear with me thanks Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017245 Share on other sites More sharing options...
trq Posted February 24, 2010 Share Posted February 24, 2010 The question was answered several replies ago with examples. The entire point of classes / objects is encapsulation. Polluting an object with global variables breaks that straight away. Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017253 Share on other sites More sharing options...
KevinM1 Posted February 24, 2010 Share Posted February 24, 2010 excuse me, but im a bit confused. if im not supposed to use globals, then what i should do in a situation like this? i like to return "Test" with my getName function. i know my questions is very newbie, please bear with me thanks Simply pass the outer variable to the object via a function: class Person { private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } $test = "test" $person = new Person(); $person->setName($test); echo $person->getName(); Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017360 Share on other sites More sharing options...
trq Posted February 24, 2010 Share Posted February 24, 2010 excuse me, but im a bit confused. if im not supposed to use globals, then what i should do in a situation like this? i like to return "Test" with my getName function. i know my questions is very newbie, please bear with me thanks Simply pass the outer variable to the object via a function: class Person { private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } $test = "test" $person = new Person(); $person->setName($test); echo $person->getName(); Well said. Did you see this reply? Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017758 Share on other sites More sharing options...
KevinM1 Posted February 24, 2010 Share Posted February 24, 2010 excuse me, but im a bit confused. if im not supposed to use globals, then what i should do in a situation like this? i like to return "Test" with my getName function. i know my questions is very newbie, please bear with me thanks Simply pass the outer variable to the object via a function: class Person { private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } $test = "test" $person = new Person(); $person->setName($test); echo $person->getName(); Well said. Did you see this reply? LMAO, nope. That will teach me to jump directly to the last message or two in a thread. Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017764 Share on other sites More sharing options...
trq Posted February 24, 2010 Share Posted February 24, 2010 Funny, the example is almost word perfect accept I always pre-pend private vars with an underscore. That was pretty much how I figured you didn't see it. Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017766 Share on other sites More sharing options...
roopurt18 Posted February 24, 2010 Share Posted February 24, 2010 Not to beat this topic to death, but I'm going to request to elnaz that you forget the PHP global keyword exists. If you ever, ever find yourself wanting to type global in your PHP code, then you need to come back here and ask us what the better way is. Globals are worse than murdering kittens and puppies and Hell won't even admit people who use them. Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017768 Share on other sites More sharing options...
elnaz1010 Posted February 25, 2010 Author Share Posted February 25, 2010 Not to beat this topic to death, but I'm going to request to elnaz that you forget the PHP global keyword exists. If you ever, ever find yourself wanting to type global in your PHP code, then you need to come back here and ask us what the better way is. Globals are worse than murdering kittens and puppies and Hell won't even admit people who use them. hehe, you remind me of my "basic" language teacher, who was talking like you about using "goto" statement ok mate, i will remember that thanks all for helping me have a good day Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017835 Share on other sites More sharing options...
roopurt18 Posted February 25, 2010 Share Posted February 25, 2010 I can't really decide which is worse, globals or goto. In any case, they don't exist and we don't know what they are! We don't even talk about them! What was this thread about again? Quote Link to comment https://forums.phpfreaks.com/topic/193174-beginner-question-accesing-outer-variables-inside-a-class/#findComment-1017867 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.