1internet Posted March 17, 2013 Share Posted March 17, 2013 I am trying to access a variable through a class, and can't work out how to this. class test { function method() { if (isset({$testing})) { echo {$testing}; } } } $testing = 'it works'; $thetest = new test; $thetest->method(); Is this even possible, or have I got very confused? Link to comment https://forums.phpfreaks.com/topic/275786-accessing-a-variable-from-procedural-through-a-class/ Share on other sites More sharing options...
Jessica Posted March 17, 2013 Share Posted March 17, 2013 You'd have to pass it to the method. Link to comment https://forums.phpfreaks.com/topic/275786-accessing-a-variable-from-procedural-through-a-class/#findComment-1419183 Share on other sites More sharing options...
KevinM1 Posted March 17, 2013 Share Posted March 17, 2013 It's both possible, and you're very confused. In PHP, methods and functions are essentially the same thing (notice how they start with the function keyword). How do you pass a variable to a function? Through its argument list. The same thing applies here: class test { public function method($x) { echo $x; } } $testing = 'it works'; $test = new test; $test->method($testing); EDIT: Note that while methods and functions are essentially the same mechanically, OOP is NOT merely placing a bunch of functions in a class and calling it a day. Link to comment https://forums.phpfreaks.com/topic/275786-accessing-a-variable-from-procedural-through-a-class/#findComment-1419184 Share on other sites More sharing options...
1internet Posted March 17, 2013 Author Share Posted March 17, 2013 Ok so what if the variable may not be set? class test { public function method($x=null) { if(isset($x)) echo $x; } } $testing = 'it works'; $test = new test; $test->method($testing); Would that work? Link to comment https://forums.phpfreaks.com/topic/275786-accessing-a-variable-from-procedural-through-a-class/#findComment-1419186 Share on other sites More sharing options...
KevinM1 Posted March 17, 2013 Share Posted March 17, 2013 Try it and see. Don't be afraid to write little test scripts. You'll learn more that way than asking us whether or not something relatively simple can work. Link to comment https://forums.phpfreaks.com/topic/275786-accessing-a-variable-from-procedural-through-a-class/#findComment-1419188 Share on other sites More sharing options...
1internet Posted March 17, 2013 Author Share Posted March 17, 2013 Yea, I do, but sometimes I just like to make sure there isn't some standard or much simpler way to do things. It didn't work though. So I think this is the best way class test { function method($x) { echo $x; } } $thetest = new test; if (isset($testing)) $thetest->method($testing); Link to comment https://forums.phpfreaks.com/topic/275786-accessing-a-variable-from-procedural-through-a-class/#findComment-1419189 Share on other sites More sharing options...
Strider64 Posted March 17, 2013 Share Posted March 17, 2013 <?php class Test { public $greetings; // Public Variable can be used anywhere public function my_world($x) // Global Method { echo "Good Afternoon, " . $x . "!<br />"; return $this->greetings = "Have a Great Day " . $x . "!<br />"; // returns a varialble to the calling method } } $thetest = new Test; // Creat a new instance $goodbye = $thetest->my_world("Kevin"); // Call the Method echo $goodbye; //display it. ?> Link to comment https://forums.phpfreaks.com/topic/275786-accessing-a-variable-from-procedural-through-a-class/#findComment-1419197 Share on other sites More sharing options...
KevinM1 Posted March 17, 2013 Share Posted March 17, 2013 <?php class Test { public $greetings; // Public Variable can be used anywhere public function my_world($x) // Global Method { echo "Good Afternoon, " . $x . "!<br />"; return $this->greetings = "Have a Great Day " . $x . "!<br />"; // returns a varialble to the calling method } } $thetest = new Test; // Creat a new instance $goodbye = $thetest->my_world("Kevin"); // Call the Method echo $goodbye; //display it. ?> 1. Public does not mean global. 2. Methods/functions really shouldn't echo and return. Link to comment https://forums.phpfreaks.com/topic/275786-accessing-a-variable-from-procedural-through-a-class/#findComment-1419198 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.