newb Posted October 8, 2006 Share Posted October 8, 2006 eh how come this happens...the code doesn't echo what variable 'a' is set to..why?...[code]<?phpclass variable {function loadvars() {$a = 'lol';}function newfunction() {$this->loadvars();echo $a;}}$variable = new variable();$variable->newfunction();?>[/code] Quote Link to comment Share on other sites More sharing options...
AliasXNeo Posted October 8, 2006 Share Posted October 8, 2006 What did you expect to happen? You must be new to classes. In a function, variables that are not set with global are completely local to the function. So if you made a function that set some random variable, then made another that printed that variable out, it wouldn't work because the random variable you made was only local to the first function, and not the second. Now, in classes you can create variables that are local to the class, which would also make them local to all the functions inside that class. Here is an example:[code]<?phpclass test { var $testing = ""; function set_test() { $this->testing = "testing"; // Set's our local variable $testing to "testing" } function echo_test() { echo($this->testing); // Echo's our local variable testing }}$class = new test();$class->set_test(); // Set's our $testing variable to "testing"$class->echo_test(); // Displays "testing"?>[/code]As you see we simply reference our local class variable using $this, as you would reference a local function. Did that clear things up? Quote Link to comment Share on other sites More sharing options...
newb Posted October 8, 2006 Author Share Posted October 8, 2006 Ohh ok, thanks alot. and yes, im new to functions and classes so that helped a bunch Quote Link to comment 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.