title: Proper OOP PHP Class structure and DB interaction
body: <p>I am fairly new to OOP and PHP. I worked my way through Kevin Yank's book and wanted to keep advancing to some OOP concepts. But I am a bit lost and I need help.</p>
  
  <p>I am trying to generate a list of vendors from the DB using a Vendor Class.
  Fields such as city and country use an ID (relational DB using a city and country table), so I will also need to be able to grab the name values for those before I publish the complete list in my html output file.</p>
  
  <p>I can do all this procedurally, but struggling to find the right structure when moving to OOP.</p>
  
  <p>I followed Kevin's instructions and created a db.inc.php file for my PDO db connection:</p>
  
  <pre><code>&lt;?php 
  
  //db connection using PDO class object
  
  try {
  
      //create new PDO class object
      $pdo = new   
      PDO('mysql:host=122.2.2.2;port=3307;dbname=mydbname','dbuser','password');
      $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $pdo-&gt;exec('SET NAMES "utf8"');
  
  }
  catch (PDOException $e)
  {
      //if unable to connect, tell us why
      $output = 'Unable to connect to the database server 2.'.$e-&gt;getMessage();
      include 'output.html.php';
      exit();
  }
  </code></pre>
  
  <p>I created a Vendor Class. This class will hold all the methods to manipulate vendors: getVendors(), getVendor($id), deleteVendor($id), updateVendor($id), etc.</p>
  
  <pre><code>&lt;?php
  
  
  class Vendor {
  
      protected $name;
      protected $address;
      protected $city;
      protected $state;
      protected $postal;
      protected $country;
      protected $tel;
      protected $email;
      protected $web;
  
  
      public function __construct(){
  
          //typically first function to appear in a class
          // Constructor using MagicMethod
          $this-&gt;name = $name;
          $this-&gt;address = $address;
          $this-&gt;city = $city;
          $this-&gt;state = $state;
          $this-&gt;postal = $postal;
          $this-&gt;country = $country;
          $this-&gt;tel= $tel;
          $this-&gt;email = $email;
          $this-&gt;web = $web;
  
      }
  
      public  function getVendors($pdo){
  
          //get all vendor data from db
          try
          {
              //prepares a statement for execution and returns a statement object
              $query = $pdo-&gt;prepare("SELECT * FROM vendor ORDER BY name");
              $query-&gt;execute();
  
              $vendors = array();
  
              foreach ($query-&gt;fetchAll() as $row){
  
              //create array of member objects
              $vendors[] = new Vendor($row);
          }
  
          //return member objects
          //not sure this is the right thing to do here
          return $vendors;
  
          }
          catch (PDOException $e)
          {
               //handle any errors
               $output = 'Error fetching vendors: ' . $e-&gt;getMessage();
               include 'output.html.php';
               exit();
  
          }//end try catch
      }//end function
  
  
      //setters
  
      public function setName( $name ){
          $this-&gt;name = $name;
      }
  
      public function setAddress( $address ){
          $this-&gt;address = $address;
      }
  
      public function setCity( $city ){
          $this-&gt;city = $city;
      }
  
      public function setState( $state ){
          $this-&gt;state = $state;
      }
  
      public function setPostal( $postal ){
          $this-&gt;postal = $postal;
      }
  
      public function setCountry( $country ){
          $this-&gt;country = $country;
      }
  
      public function setTel( $tel ){
          $this-&gt;tel = $tel;
      }
  
      public function setEmail( $email ){
          $this-&gt;email = $email;
      }
  
      public function setWeb( $web ){
          $this-&gt;web = $web;
      }
  
  
      //getters
  
      public function getName(){
          return $this-&gt;name;
      }
  
      public function getAddress(){
          return $this-&gt;address;
      }
  
      public function getCity(){
          return $this-&gt;city;
      }
  
      public function getState(){
          return $this-&gt;state;
      }
  
      public function getPostal(){
          return $this-&gt;postal;
      }
  
      public function getCountry(){
          return $this-&gt;country;
      }
  
      public function getTel(){
          return $this-&gt;tel;
      }
  
      public function getEmail(){
          return $this-&gt;email;
      }
  
      public function getWeb(){
          return $this-&gt;web;
      }
  
  }
  </code></pre>
  
  <p>I have an index.php file that is calling the list of vendors</p>
  
  <pre><code>&lt;?php 
  
  //get all vendors
  
  include_once $_SERVER['DOCUMENT_ROOT'] .'/db.inc.php'; 
  include_once $_SERVER['DOCUMENT_ROOT'] .'/includes/Vendor.Class.php'; 
  
  //instantiate class
  $vendor = new Vendor();
  
  //calling my function to get a list of all vendors
  //not sure if passing in $pdo object is a good idea/practice
  
  $vendors = $vendor-&gt;getVendors($pdo);
  
  //test print out vendors
  echo $vendors;
  print_r($vendors);
  
  //output to template
  include 'output.html.php';
  </code></pre>
  
  <p>The idea is to then use a template/output html file to output data:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
      &lt;head&gt;
  
      &lt;/head&gt;
  
      &lt;body&gt;
  
      &lt;?php foreach ($vendors as $vendor){
  
          //if my city is an id value, at what point do I call the City Class to look up   
          that city name? I should not be calling DB from my view. And is it OK to have  
          html code in my controller?
  
          $name = $vendor-&gt;getName();
          $address2 = $vendor-&gt;getAddress2();
          $city = $vendor-&gt;getCity();
          $zip = $vendor-&gt;getZip();
          $web = $vendor-&gt;getWeb();
          $phone = $vendor-&gt;getTel();
  
  
      ?&gt;
  
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>My questions:</p>
  
  <ol>
  <li>In my vendor class I am creating an array of Vendor Objects - is this the correct OOP way of returning my rows of Vendors from DB?</li>
  <li>If my country and city are IDs, and each has their own class, when and where is the appropriate time to call those classes and use getCity($id) and getCountry($id) to get name values instead of ids, and work them into my foreach statement?</li>
  </ol>
  
  <p>Any assistance is greatly appreciated.</p>