ChadNomad Posted December 20, 2007 Share Posted December 20, 2007 Hi, I'm still pretty new to OOP and have just started writting my first registration class. Everything is going well except I can't seem to pass a variable from one function to another. For example if in function a() $test = "hello" it won't work if I use it in function b(). How do I make them "global"? I don't quite understand the what global vars actually are but apparently it's bad practice and I haven't had much luck in my tests with them. I also only want it to be "global" in the class I've created. Hope that wasn't too confusing! Would $this-> work? <- only just thought of that after posting. Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 20, 2007 Share Posted December 20, 2007 You would use the $this keyword, yes. <?php Class A{ var $foo; function bar(){ $this->foo = 'Hi'; } function myFunc(){ $this->foo = 'Hello'; } ?> Quote Link to comment Share on other sites More sharing options...
dbillings Posted December 20, 2007 Share Posted December 20, 2007 She's too quick for me. <?php class a { private $test; function a() { $this->test = "hello"; } function b() { echo $this->test; } } ?> Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted December 20, 2007 Share Posted December 20, 2007 You can also (if you have 2 classes) have the class you want to grab the variables, and inherit the first class. Quote Link to comment Share on other sites More sharing options...
ChadNomad Posted December 21, 2007 Author Share Posted December 21, 2007 Thanks. Function to function is fine now. Still... I'm now trying to use a variable outside of both the function and class. echo $registration->errorString; The above does nothing. I've included the class in an external file. At the bottom of the class file I have: $registration = new Registration(); Am I doing something wrong?? 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.