Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Config file in Trait?

Status
Not open for further replies.

ThomasJSmart

Programmer
Sep 16, 2002
634
Just wondering if there would be anything against using a trait to store configuration settings for an application?

Code:
trait CONFIG{
  private $db_login = 'mylogin';
  private $db_pasw  = 'mypasw';

  public function get_db_auth(){
    return array($db_login,$db_pasw);
  }
}


class DB{
  use CONFIG

  public function connect(){
    $db_auth = $this->get_db_auth();
    $db = my_db_connect('localhost',$auth[0],$auth[1]);
    return $db
  }
}


$db_obj = new DB();
$db_con = $db_obj->connect();

And secondly, was wondering if there was a way to only let specific classes call specific functions in a trait.
so you could say only the DB class can call the get_db_auth function in the above example or that it only returns a result for the db class and for other classes it would return a blank array.


site | / blog |
 
hmm i realize now that for the above the function isnt even needed, can just open the variables directly so would need to put them inside the function. then for the second part i could check the current class name and limit to that.

Code:
trait CONFIG{
  public function get_db_auth(){
		$db_login = 'mylogin';
  	$db_pasw  = 'mypasw';
	
		$access = array('DB');
		if(in_array(get_class(),$access)){
    	return array($db_login,$db_pasw);
		}else{
			return array();
		}
  }
}


class DB{
  use CONFIG;

  public function connect(){
    $db_auth = $this->get_db_auth();
		return $db_auth;
  }
}

class UNSAFE{
  use CONFIG;

  public function connect(){
    $db_auth = $this->get_db_auth();
		return $db_auth;
  }
}


$DB = new DB();
$auth = $DB->connect(); 
print_r($auth);

$UNSAFE = new UNSAFE();
$auth = $UNSAFE->connect(); 
print_r($auth);


the DB correctly gets the db auth details and the UNSAFE does not have access



site | / blog |
 
further updated.
anyone any thoughts on security or best practice related reasons not to do this or to do it differently / improve upon it?

Code:
<?PHP


trait CONFIG{
	
  private function db_auth(){
		
    // settings
    $return = array(
      'db_login'  => 'mylogin',
      'db_pasw'   => 'mypasw',
      'db_host'   => 'localhost'		
    );
		
    // classes with access to these settings
    $access = array(
      'DB',
      'SDB'
    );
		
    return $this->return_settings($return,$access);
  }
	
	
  private function timeout(){
		
    // settings
    $return = array(
      'timeout'  => 2000	
    );
		
    return $this->return_settings($return);
  }
	
	
  protected function config($type){
    return $this->$type();
  }
	
  private function return_settings($return,$access=array()){
    if(!empty($access) && !in_array(get_class(),$access) && !in_array(get_parent_class(),$access)){
      return array();
    }
		
    return $return;
  }
}


class DB{
  use CONFIG;

  public function connect(){
    return $this->config('db_auth');
  }
}

abstract class SDB{
  public abstract function connect();	
}

class SUBDB extends SDB{
  use CONFIG;

  public function connect(){
    return $this->config('db_auth');
  }
}

class UNSAFE{
  use CONFIG;

  public function connect(){
    return $this->config('db_auth');
  }
	
  public function timed(){
    return $this->config('timeout');
  }
}


// has access, gets auth details
$DB = new DB();
echo 'DB:<br>';
print_r($DB->connect()); 
echo '<br><br>';

// parent has access, gets auth details
$SUBDB = new SUBDB();
echo 'SUBDB:<br>'; print_r($SUBDB->connect());		
echo '<br><br>';

// does not have access, no details
$UNSAFE = new UNSAFE();
echo 'UNSAFE:<br>'; print_r($UNSAFE->connect());
echo '<br><br>';

// anyone has access, show timeout
echo 'UNSAFE TO:<br>'; print_r($UNSAFE->timed());
echo '<br><br>';

Outputs
DB:
Array ( [db_login] => mylogin [db_pasw] => mypasw [db_host] => localhost )

SUBDB:
Array ( [db_login] => mylogin [db_pasw] => mypasw [db_host] => localhost )

UNSAFE:
Array ( )

UNSAFE TO:
Array ( [timeout] => 2000 )

site | / blog |
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top