title: PHP Pass an Object as a Parameter via the Constructor
body: <p>I'm trying to pass an object to another via constructor. I have a class that gathers up the database credentials and assigns them to the class properties. Then another class that does the actual query and returns an array of rows. Here's the code:</p>
  
  <pre><code>// GetCredentials
  class GetCredentials
  {
      public function __construct()
      {   
      }
  
      private $_db = "";
      private $_uname = "";
      private $_pwd = "";
  
      public function DB($value = NULL)
      {
          if( $value===NULL )
              return $this-&gt;_db;
          else
              $this-&gt;_db = $value;
      }
      public function Uname($value = NULL)
      {
          if( $value===NULL )
              return $this-&gt;_uname;
          else
              $this-&gt;_uname = $value;
      }
      public function Pwd($value = NULL)
      {
          if( $value===NULL )
              return $this-&gt;_pwd;
          else
              $this-&gt;_pwd = $value;
      }
  
      public function readCreds($filename)
      {
          $parray = array();
          $file = fopen($filename,"r");
          while(! feof($file))
            {
            array_push($parray,fgets($file));
            }
          fclose($file);
          $this-&gt;DB = $parray[0];
          $this-&gt;Uname = $parray[1];
          $this-&gt;Pwd = $parray[2];
      }   
  
  }
  
  
  // DBconnection
  class DBconnection{
      public function __construct(GetCredentials $cr)
      {   
          $Uname = $cr-&gt;Uname;
          $DB = $cr-&gt;DB;
          $Pwd = $cr-&gt;Pwd;
          }
  
      private $conn;
      private $Uname;
      private $DB;
      private $Pwd;
  
      public function dbConnect($sql)
      {
  
      try {
      $conn = new PDO("mysql:host=localhost;dbname=" . $DB, $Uname, $Pwd);
      $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $arr = $conn-&gt;prepare($sql);
      $arr-&gt;execute();
      $result = array();
      $result = $arr-&gt;fetchAll();
      return $result;
          }
      catch(PDOException $err) {
      return "ERROR: Unable to connect: " . $err-&gt;getMessage();
          }
      }
      function __destruct(){
          $conn = null;
      }           
  }
  </code></pre>
  
  <p>To test I've instantiated the GetCredentials class, populated the values, then passed to the DBConnection object values and all and have it do the actual data retrival.
      My test script looks like this:</p>
  
  <pre><code>error_reporting(E_ALL | E_STRICT);
  ini_set('display_errors', true);
  ini_set('auto_detect_line_endings', true);
  
  
  $sC = New GetCredentials();
  $sC-&gt;readCreds('dbdata.txt');
  
  $db = New DBConnection($sC);
  $sql = 'SELECT `Member ID`, `Address 1`, City, State, Zip FROM `MemberDataAll` WHERE LENGTH(Zip) &gt;= 5';
  
  $rArr = $db-&gt;dbConnect($sql);
  echo count($rArr);
  </code></pre>
  
  <p>My script throws a set of errors that state the properties of the <code>DBConnection object</code> are empty. I'm thinking it might be that my utilization is incorrect but I'm not sure. I'm also wondering if using $this in the constuctor is the best idea, is <code>Self::</code> better and if so why? Can anyone set me straight? Thanks in advance.</p>