开发者

geting a variable from public scope to connect database part 2

$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 i开发者_StackOverflow社区m 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 getting a variable from a public scope to connect database. but im trying with different problems.

thanks for looking in.


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.


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);     
   }

}


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);


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);

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜