Dubreuilmedia wrote:
Hi
I was wondering about includes and classes. I have a class in which
depending on a member variable, i should load
the proper include, which is really the proper config file for that moment.
How does a member variable control this? E.g. you set the member variable in the constructor and then include some config file?
Now if i do this in the constructor
i can't seem to be able to use the variables found in my config in the other
member functions.
Yeah, this can be a little confusing. When you include a file in a function, it only gains the scope of that function. I.e. variables in the included file are only available to that function. What you can do, however, is to assign the config variables to an array in the object.
<?php
class someClass { function someClass($param) {
// Include the config file if (21 == $param) { include_once 'config21.php'; }
// Bring the config variables into this object foreach ($config as $varname => $value) { $this->config['varname'] = $value; } } }
?>
Obviously the above method is a little messy. A better way to handle this would be object overloading with __set() and __get(), which is available in PHP4 with a few commands and PHP5 uses it by default.
http://www.php.net/overload
(I wouldn't be too afraid of the experimental warning, overloading in PHP5 is pretty much the same as in PHP4, and looks like it's here to stay).
Or in your config.php use $GLOBALS['varName'] = 'value'; That would clear the whole thing up without extra storing in the object.
-- paperCrane <Justin Patrin>
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php