Anti-Moronic Posted June 22, 2009 Share Posted June 22, 2009 I have this variable: $string = "string"; I want it to be accessible within my class and function: class someclass{ function speak(){ echo $test; } } Currently, I define $test as global within the func, and all is ok, I can now use it within that func. My question is, how secure is this, does this have anything to do with register_globals? Is there an alternative? Remember, I can't pass these variables into the function. Thanks. Link to comment https://forums.phpfreaks.com/topic/163212-globals-pass-variable-to-class-without-passing-through-function-par-list/ Share on other sites More sharing options...
trq Posted June 22, 2009 Share Posted June 22, 2009 Remember, I can't pass these variables into the function. Why? The point of classes is encapsulation. Relying on global variables breaks this completely. Link to comment https://forums.phpfreaks.com/topic/163212-globals-pass-variable-to-class-without-passing-through-function-par-list/#findComment-861141 Share on other sites More sharing options...
Anti-Moronic Posted June 22, 2009 Author Share Posted June 22, 2009 My question is, how secure is this, does this have anything to do with register_globals? Is there an alternative? Link to comment https://forums.phpfreaks.com/topic/163212-globals-pass-variable-to-class-without-passing-through-function-par-list/#findComment-861143 Share on other sites More sharing options...
trq Posted June 22, 2009 Share Posted June 22, 2009 How secure is what? The code you posted doesn't work, nor does it have anything to do with register_globals. Link to comment https://forums.phpfreaks.com/topic/163212-globals-pass-variable-to-class-without-passing-through-function-par-list/#findComment-861145 Share on other sites More sharing options...
gevans Posted June 22, 2009 Share Posted June 22, 2009 Hey, I think you want something like this. class fooClass { function speak($bar) { echo $bar; } } $foo_class = new fooClass; $foo = "bar"; $foo_class->speak($foo); That sends a variable from you script to the class method 'speak'. And then you do whatever you want with it. Unless the string assigned to $foo is coming from a $_POST, $_GET or other request method this has nothing to do with register_globals. This is not good class syntax, jsut an example. Link to comment https://forums.phpfreaks.com/topic/163212-globals-pass-variable-to-class-without-passing-through-function-par-list/#findComment-861147 Share on other sites More sharing options...
Anti-Moronic Posted June 22, 2009 Author Share Posted June 22, 2009 I know, I pass variables to functions all the time. What I'm asking is, does this: $test = "hello"; class{ function speak(){ global $test; echo $test } } ...have anything to do with register_globals? By making the $test variable global? Thanks. Thorpe: "Currently, I define $test as global within the func." Link to comment https://forums.phpfreaks.com/topic/163212-globals-pass-variable-to-class-without-passing-through-function-par-list/#findComment-861151 Share on other sites More sharing options...
trq Posted June 22, 2009 Share Posted June 22, 2009 ...have anything to do with register_globals? By making the $test variable global? No, but it also defeats the purpose of using objects. Link to comment https://forums.phpfreaks.com/topic/163212-globals-pass-variable-to-class-without-passing-through-function-par-list/#findComment-861154 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.