views:

41

answers:

4
$hostname['application'] = '127.0.0.1';
$username['application'] = 'root';
$password['application'] = 'root';
$database['application'] = 'band';
$dbdriver['application'] = 'mysql';

class database
{
    private $hostname;
    private $username;
    private $password;
    protected $database;
    private $dbdriver;

    function __construct( $database )
    {

    }
}

$db = new database( 'application' );

still same question as before, but im still want to learn about how to make a simple pdo wrapper. can we somehow get $hostname['application'] , etc by just knowing the $x['database'] ? i mean what im trying to do is putting http://stackoverflow.com/questions/3880904/getting-a-variable-from-a-public-scope-to-connect-database. but im trying with different problems.

thanks for looking in.

+2  A: 

Try dependency injection instead:

$hostname['application'] = '127.0.0.1';
$username['application'] = 'root';
$password['application'] = 'root';
$database['application'] = 'band';
$dbdriver['application'] = 'mysql';

class database {
    private $hostname;
    private $username;
    private $password;
    protected $database;
    private $dbdriver;

    function __construct( $hostname, $username, $password, $database, $driver = 'mysql' ) {
        $this->hostname = $hostname;
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;
        $this->driver = $driver;
    }
}

$dbInstance = new database( $hostname['application'], $username['application'], $password['application'], $database['application'] );

As a general rule, you should not use global variables. If you need a variable somewhere within a function or class method, you should explicitly 'inject' them as an argument to your function/method.

Jacco
i dont get it , the diffrence between DI and useing global as above?
Adam Ramadhan
In PHP functions, methods and classes have their own 'namespace'; think of this as separate rooms in your program. Every room has a front door (the function call) and a back door (the return value). Good practice is to send incoming variables through the front door (as arguments) and receive the result at the back door (as return value). If you stick to this principle, you don't have to worry about what happens inside the room, just about the front- and back door.
Jacco
If you start using global variables, you create a giant hole in the walls of your room and start introducing new variables somewhere in the middle of your function flow. This means that I need to look at how the room (=function or method) 'works' to figure out what to expect as return value. And maybe, the function even modifies global variables somewhere, I just can't be sure. So I have to examine every line to see what happens.
Jacco
"Global variables are (potentially) evil because they are accessible from every function in every file. They can be shadowed by local variables and, in the worst case, modified by multiple different functions. This increases complexity, the potential for bugs, reduces ease of code maintenance and increases the chances of a long and painful bug hunt." So in general, you should not use global variables.
Jacco
I agree with Jacco here. If you really needed something to be global it would be better to make some kind of registry calls or a singleton are that single instace holds all the "globals" this way you have more control.
prodigitalson
A: 

You're doing it wrong.

Try:

$dbc['hostname'] = '127.0.0.1';
$dbc['username'] = 'root';
$dbc['password'] = 'root';
$dbc['database'] = 'band';
$dbc['dbdriver'] = 'mysql';

$db = new database($db);
fabrik
+1  A: 

Well you should be able to grab it from $_GLOBALS as mentioned in the other thread. if you restructure your settings to something like:

$settings = array(
  'application' => array(
     'db' => array(
       'dbname' => 'band',
       'driver' => 'mysql',
       'user' => 'root',
       'password' => 'root',
       'host' => '127.0.0.1'
     )
  )
);

Then you could easily do what youre talking about if i understand you correctly... for example:

class Database
{
    protected $hostname;
    protected $username;
    protected $password;
    protected $database;
    protected $driver;
    protected $dbname;


    function __construct( $database, $options = array())
    {
       $options = array_merge($_GLOBALS['settings'][$application]['db'], $options);
       $this->setOptions($options);
    }

    public function getConnection()
    {
       if(!$this->database)
       {
          $this->database = new PDO($this->getDsn(), $this->username, $this->password);
       }

       return $this->database;
    }

   public function setOptions(array $options)
   {
       foreach($options as $name => $value)
       {
          $method = 'set'.$name;
          if(method_exists($this, $method))
          {
             $this->$method($value);
          }
       }
   }

   public function setHost($host)
   {
     $this->host = $host;
   }

   public function setUsername($username)
   {
     $this->username = $username;
   }

   public function setPassword($password)
   {
     $this->password = $password;
   }

   public function setDriver($driver)
   {
     $this->driver = $driver;
   }

   public function setDbname($dbname)
   {
     $this->dbname = $dbname;
   }

   public function getDsn()
   {
      return sprintf('%s:host=%s;dbname=%s', $this->driver, $this->host, $this->dbname);     
   }

}
prodigitalson
wew i never thought of that before. thanks. putting ['application'] in settings. thanks!
Adam Ramadhan
btw can we call this DI too ?
Adam Ramadhan
No, using global variables is, by definition, not equal to dependency injection.
Jacco
A: 

try something like this:

<?php

final class Constants 
{
    const DB_CHARSET    = "latin1";
    const DB_TIMEOUT    = 15;

    const DB_PORT       = 3306;
    const DB_HOST       = "127.0.0.1";
    const DB_DATABASE   = "foo_db";
    const DB_USER       = "foo_dbo";
    const DB_PASS       = "pass";

    private function __construct(){}
}

final class Database
{
    private $Host = null;
    private $Database = null;
    private $User = null;
    private $Pass = null;
    private $Port = null;

    private $Opened = false;
    private $InTrans = false;

    private $Conn = null; //the connection or link

    public function __construct($conn){
        $this->Host = $conn["host"];
        $this->Database = $conn["database"];;
        $this->User = $conn["user"];;
        $this->Pass = $conn["pass"];;
        $this->Port = $conn["port"];;
    }

    public function Open(){
        //open the database connection as late as possible
    }

    public function Close(){
        //close the database connection as soon as possible
    }

    //lots more methods

}

$conn = array("host"=> Constants::DB_HOST, "user"=> Constants::DB_USER, "pass"=> Constants::DB_PASS, 
             "database"=>Constants::DB_DATABASE, "port"=>Constants::DB_PORT);

$db = new Database($conn);

var_dump($db);

?>
f00