DamienRoche Posted October 14, 2008 Share Posted October 14, 2008 How to the hell do I use a variable that has been previously decalred within a function (method) within a class? <?php $var = "sometext"; class getvar{ function var() { echo "your var: $var"; } } ?> NOTE: class may be faulty- I wrote it on the spot. I have one working. Love to know how to tackle this. It seems this damn variable scope just gets in the way. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/ Share on other sites More sharing options...
Bendude14 Posted October 14, 2008 Share Posted October 14, 2008 This seems to work for me providing you change the name of your function and make the $var global inside the function var is a reserved word, i dont think the use of it is recommended now in php 5 but it was in php4 <?php error_reporting(E_ALL); $var = "sometext"; class getvar{ function test() { global $var; echo "your var: $var"; } } $obj = new getvar(); $obj->test(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-664912 Share on other sites More sharing options...
trq Posted October 14, 2008 Share Posted October 14, 2008 The entire point of objects is to encapsulate data, dragging data in from outside like that is a bad idea. You'll want to pass the variable to your method. <?php class getvar { public function test($var) { echo "your var: $var"; } } $var = "sometext"; $obj = new getvar(); $obj->test($var); ?> Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-664952 Share on other sites More sharing options...
discomatt Posted October 14, 2008 Share Posted October 14, 2008 Alternately: <?php $var = "sometext"; class getvar { protected $var; public function __construct( $var ) { $this->var = $var; } public function test() { echo "Var: {$this->var}"; } } $obj = new getvar( $var ); $obj->test(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-665026 Share on other sites More sharing options...
nadeemshafi9 Posted October 14, 2008 Share Posted October 14, 2008 The entire point of objects is to encapsulate data, dragging data in from outside like that is a bad idea. You'll want to pass the variable to your method. <?php class getvar { public function test($var) { echo "your var: $var"; } } $var = "sometext"; $obj = new getvar(); $obj->test($var); ?> in theory yes for widley used components out in the open, but if ur gona use ur components only for your systems you can do some clever stuff, my site completley changes its data sets based on one variable which alters all the data access classes, therfore becoming a different site in the same db, basicaly i people freez the site into a project id and add that project id into new records which are inturn altered to compensate new relations big ass script 600 users. looking back on it i should have done what u said passed the data to the classes, but oh well lol. i would have passed the data to teh classes im trained in 2004-2007 so i know but the bespoke systems already have messed up ways Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-665033 Share on other sites More sharing options...
discomatt Posted October 14, 2008 Share Posted October 14, 2008 in theory yes for widley used components out in the open, but if ur gona use ur components only for your systems you can do some clever stuff, my site copletley changes its data sets based on one variable which alters all the data access classes, therfore becoming a different site in the same db. You can do this just as easily without mucking around in the global namespace. There's really no reason to use it in an object-oriented system ( beyond the top-level pages, obviously ) Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-665041 Share on other sites More sharing options...
nadeemshafi9 Posted October 14, 2008 Share Posted October 14, 2008 in theory yes for widley used components out in the open, but if ur gona use ur components only for your systems you can do some clever stuff, my site copletley changes its data sets based on one variable which alters all the data access classes, therfore becoming a different site in the same db. You can do this just as easily without mucking around in the global namespace. There's really no reason to use it in an object-oriented system ( beyond the top-level pages, obviously ) my manager got carried away with config files and thats where this came from, the aim is to have the config file read from a databse instead of a php file, so basicaly all teh defined data will be read into arrays and vars from a db empowering teh application to be sold in a box. Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-665049 Share on other sites More sharing options...
nadeemshafi9 Posted October 14, 2008 Share Posted October 14, 2008 i have had alot of fun with java and i do understand but things often lead there own way unless you refactor ur code constantly. i work on about 30 sites with 600-1000 users and over 10 000 records each and am developing new sites every day with the same framework so yes our components evolve function > class the platform it was developed on is php 4 with myswl 4 but its hosted and now developed on a mixture of both so i need to compensate etc, but overall i agree full object orientation is teh way forward for reusability which is what we all need. Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-665052 Share on other sites More sharing options...
PFMaBiSmAd Posted October 14, 2008 Share Posted October 14, 2008 Everything starting with post #4 in this thread is off topic and has nothing to do with the problem that original poster has and is not helping him fix it. Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-665064 Share on other sites More sharing options...
nadeemshafi9 Posted October 14, 2008 Share Posted October 14, 2008 Everything starting with post #4 in this thread is off topic and has nothing to do with the problem that original poster has and is not helping him fix it. im sorry i disagree, were talkign about how people can get to a point where they have used global variables in there classes and detering the next generation that will read these posts in years to come and leaving them a trail of history, if u wana be a history wiper like some other people in history u go ahead. Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-665070 Share on other sites More sharing options...
nadeemshafi9 Posted October 14, 2008 Share Posted October 14, 2008 The most suggested line of PHP code that helps pinpoint why code is not working - error_reporting(E_ALL); << true Debugging step #1: To get past the garbage-out=garbage-in stage in your code, you must check that the inputs to your code are what you expect. << true The answers you see in a forum are not secrets. They were learned by reading documentation. << False some things are creative 2/3 ok for a junior lol joke Quote Link to comment https://forums.phpfreaks.com/topic/128360-damn-you-variable-scope/#findComment-665078 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.