iarp Posted April 12, 2009 Share Posted April 12, 2009 I'm having trouble with variables and accessing them within classes. I have $L['base'] = '/home/'; at the start of my script and i need to access it within a class i've built a bit furthur down the page. Parse error: parse error, expecting 'T_FUNCTION' in <LOCATION> on line 3 Line 3 pertains to <?php class systemsettings { global $L; ...etc etc... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/153778-solved-variable-scope/ Share on other sites More sharing options...
iarp Posted April 12, 2009 Author Share Posted April 12, 2009 without global $L at the top. It says undefined variable on line 51 and line 51 is within the class using $L['base'] Quote Link to comment https://forums.phpfreaks.com/topic/153778-solved-variable-scope/#findComment-808204 Share on other sites More sharing options...
Axeia Posted April 12, 2009 Share Posted April 12, 2009 You access class variables (outside the method) trough the use of $this-> Take this as an example <?php class MyClass { private $name; function __construct( $pName ) { $this->name = $pName; } } ?> notice how the dollar sign 'moves' to the front of $this->. Think that should be of help? Quote Link to comment https://forums.phpfreaks.com/topic/153778-solved-variable-scope/#findComment-808213 Share on other sites More sharing options...
iarp Posted April 12, 2009 Author Share Posted April 12, 2009 This is the file <?php $L['base'] = '/lemon/'; class systemsettings { function base() { return $L['base']; } } ?> but "return $L['base'];" inside the class isn't able to access the originating $L at the very start of the script. Quote Link to comment https://forums.phpfreaks.com/topic/153778-solved-variable-scope/#findComment-808220 Share on other sites More sharing options...
Axeia Posted April 12, 2009 Share Posted April 12, 2009 The logical way to go about this would be by passing the variable as a parameter like this: <?php class systemsettings { function base( $pL ) { return $pL['base']; } } $sysSettings = new systemsettings(); echo $sysSettings->base( $L ); ?> Or by passing it to the construct like this: <?php class systemsettings { private $L; function __construct( $pL ) { $this->L = $pL; } function base( $pL ) { return $this->L['base']; } } $sysSettings = new systemsettings( $L ); echo $sysSettings->base(); ?> Where the second example has the advantage that you could add another method doing something else with L['base'].. you gotta figure that one out for yourself which is the best to use for the given situation. [edit] A third (not recommended way) would be: <?php class systemsettings { public $L; function __construct( $pL ) { $this->L = $pL; } } $sysSettings = new systemsettings( $L ); echo $sysSettings->L; ?> (class variable is public instead of private making it externally accessible) Quote Link to comment https://forums.phpfreaks.com/topic/153778-solved-variable-scope/#findComment-808222 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.