CyberShot Posted October 27, 2009 Share Posted October 27, 2009 class person { var $name = "Jimmy Goe"; function get_name(){ echo $this->name; } } I am playing with this very small script that I made using a tutorial. I can get the script to echo out the name using the code above. What I don't get is why I can't do this class person { var $name; function get_name(){ $name = "Jimmy Goe" echo $this->name; } } then of course in my index page, I am calling it like this $joe = new person(); $joe->get_name(); If you decide to answer this question, please be as detailed as possible, I am a newb as you can see and the more information you give or the simpler the explanation, the better. Thanks for helping Link to comment https://forums.phpfreaks.com/topic/179153-understanding-functions-and-classes/ Share on other sites More sharing options...
joel24 Posted October 27, 2009 Share Posted October 27, 2009 i think its because of the scope of a function. i.e. function get_name(){ $name = "Jimmy Goe" echo $this->name; } $name within the function and $name in the class are two separate variables, because the scope of a variable within a function is local.. try function get_name(){ $this->name = "Jimmy Goe" echo $this->name; } Link to comment https://forums.phpfreaks.com/topic/179153-understanding-functions-and-classes/#findComment-945184 Share on other sites More sharing options...
CyberShot Posted October 27, 2009 Author Share Posted October 27, 2009 yes, that works. but it defeats the purpose of the variable in the class. What I am trying to learn here is why the variables are assigned in the class. I thought it was because they were to be properties for the methods. $this only gives access to the method and not the class right? Link to comment https://forums.phpfreaks.com/topic/179153-understanding-functions-and-classes/#findComment-945190 Share on other sites More sharing options...
trq Posted October 27, 2009 Share Posted October 27, 2009 $this refers to the instance of the class. So when you say $this->name you are referring to the name property defined within that object. Link to comment https://forums.phpfreaks.com/topic/179153-understanding-functions-and-classes/#findComment-945192 Share on other sites More sharing options...
joel24 Posted October 27, 2009 Share Posted October 27, 2009 read up on it, http://www.php.net/manual/en/language.oop5.basic.php Link to comment https://forums.phpfreaks.com/topic/179153-understanding-functions-and-classes/#findComment-945199 Share on other sites More sharing options...
CyberShot Posted October 27, 2009 Author Share Posted October 27, 2009 while reading this stuff it usually always prompts a question. I was following a tutorial where it was being explained in a video. But they always leave something out which promtps the questions Link to comment https://forums.phpfreaks.com/topic/179153-understanding-functions-and-classes/#findComment-945200 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.