Vaiable Scope
Posted: Wed Oct 13, 2004 5:03 pm
I have a nice library of classes. Many of them require config variables, which i passed to them via the constructor. Example:
This has become very tidiouse and sometimes gets in the way. Is there any way I can call a variable from within a class that was defined outside of a class? Example:
I have only know "global" to work with functions, such as
Is there anything like the above that will work with classes? Thanks!
Code: Select all
<?php
$config_vars['project_title'] = "Example Project";
$config_vars['project_version'] = "0.6";
$config_vars['db_user'] = "Example DB User";
$config_vars['db_pass'] = "Example DB Pass";
$config_vars['db_name'] = "Example DB Name";
$config_vars['db_host'] = "localhost";
class example {
public $config_vars
public function __construct ($config_vars){
$this->config_vars = $config_vars;
}
public function dplyConfVars() {
foreach ($config_vars as $index => $value) {
echo $index . ' => ' . $value . '<br>';
}
}
}
?>Code: Select all
<?php
class example {
public function __construct(){
}
public function dplyConfVars() {
global $config_vars;
foreach ($config_vars as $index => $value) {
echo $index . ' => ' . $value . '<br>';
}
}
}
?>Code: Select all
<?php
function changeVar($new_var) {
global $var;
$var = $new_var;
}
$var = 1;
echo $var; //displays 1
changeVar(5);
echo $var; //displays 5
?>