views:

44

answers:

3
<?php  
$settings['hostname'] = '127.0.0.1';
$settings['username'] = 'root';
$settings['password'] = 'root';
$settings['database'] = 'band';
$settings['dbdriver'] = 'mysql';

/**
* DATABASE
*/
class database
{
 protected $settings;
 function __construct()
 {

 }

 function connect() 
    {
  $this->start = new PDO(
  $this->settings['dbdriver'] . ':host='. 
  $this->settings['hostname'] . ';dbname='. 
  $this->settings['database'],
  $this->settings['username'],
  $this->settings['password'],
  array(PDO::ATTR_PERSISTENT => true));

  $this->start->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
    }
}
?>

ok im still a student so today im learning about scope and connections database the question is how can i put the $settings out of the class in to the protected $settings in class ? is there a way ? or maybe you have a better way of doing it ?

thanks for looking in Adam Ramadhan

A: 

A better way would be to either look in a configuration file (a XML file for example) for the configuration, or have the connect() method (or your constructor) called with the desired parameters.

Colin Hebert
+3  A: 

You are already on the right path in the code you show: Don't use public (global) scope at all - it's not regarded good practice in OOP to rely on global variables, because it breaks encapsulation. Instead, inject the settings into the object when initializing it.

You could add a constructor to do that:

function __construct($settings)
 {
   $this->settings = $settings;
 }

and then initialize the class like so:

 $database= new database($settings); 

or like so, to prevent a variable with sensitive data floating around:

 $database= new database(array('hostname' => '127.0.0.1',
                  'username' => 'root',
                  'password' => 'root',
                  'database' => 'band',
                  'dbdriver' => 'mysql'));

As a side note, in production use, consider deleting the password variable from the array after connecting, for security. It's nothing essential but a nice additional thing to do.

Pekka
Does it really do any good to unset the password variable? As soon as an attacker gathers code-execution access to your application unseting the password won't help any more. He may fire queries even without the password.
nikic
@nikic this is more for situations when a full variable dump is done from within the script (e.g. when a production app accidentally runs with development error reporting settings). I've seen frameworks doing a `var_dump` on errors in verbose mode. It's not massively essential but can't hurt either
Pekka
how do i delete the password after connection ?
Adam Ramadhan
@Adam `unset($this->settings["password"]);`
Pekka
+1  A: 

Either pass the $settings as function argument, import it to current scope using global or access via $GLOBALS.

Pass as argument:

public function __construct(array $settings) {
    $this->settings = $settings;
}

Import using global:

public function __construct() {
    global $settings;
    $this->settings = $settings;
}

Use $GLOBALS:

public function __construct() {
    $this->settings = $GLOBALS['settings'];
}

I would choose the pass as arg variant. The other versions are only dirty hacks (imho).

nikic