title: Managing several classes in php
body: <p>Trying to get my head around classes and OOP</p>
  
  <p>I have the following situation and i define my classes as such</p>
  
  <pre><code>// Include database connection class
  require_once INCLUDE_PATH.'database.class.php';
  $database = new Database;
  
  // include the functions helper class
  require_once INCLUDE_PATH.'functions.php';
  $functions = new functions();
  
  // include the error handling class
  require_once INCLUDE_PATH.'error_handling.php';
  $errorHandling = new errorHandling();
  </code></pre>
  
  <p>database.php</p>
  
  <pre><code>class Database{
  
      public function __construct(){
  
      // Create dsn connection string
          $dsn = 'mysql:host=' . $this-&gt;host . ';dbname=' . $this-&gt;dbname;
  
      $options = array(
          // avoids duplicate connections being opened
      PDO::ATTR_PERSISTENT    =&gt;  true,
      // sets error mode if a database error is thrown
      PDO::ATTR_ERRMODE       =&gt;  PDO::ERRMODE_EXCEPTION
      );  
  
      // Create a new PDO instanace
          try{
              $this-&gt;dbh = new PDO($dsn, $this-&gt;user, $this-&gt;pass, $options);
          } catch(PDOException $e){ // Catch any errors
              $this-&gt;error = $e-&gt;getMessage();
  
          // email the administrator and redirect and display a user friendly message for the user
          $errorHandling-&gt;database_connection();
          }
  
      } // end of construct function to setup database connection
  } // end of database class
  </code></pre>
  
  <p>functions.php</p>
  
  <pre><code>class functions{
  
      public function email_admin {
  
      // run code to email the administrator with the error
      }
  }
  </code></pre>
  
  <p>error_handling.php</p>
  
  <pre><code>class errorHandling{
  
  public function database_connection(){
  
      // this handles the code that holds the errors for
      // both the admin message and user message and redirects
      // the user to a page to show the user friendly error
      // if the database connection fails
      // it also runs the code to email the admin before redirecting
  
      $functions-&gt;email_admin();
  
  }
  </code></pre>
  
  <p>So you can see what im trying to do and most likely quite badly. I have extensive error handling for user and admin so im using a class for that so i don't clutter up the code. Im using a helper function and im using the database class which has all the sql stuff.</p>
  
  <p>There are several situations where a database function is using functions in both error handling and functions for emailing the admin. I could just lump everything into 1 big class and solve my issue but that wouldn't be efficient.</p>
  
  <p>I can use global $functions and global $errorHandling within each individual function when i need it but that seems repetitive. Is there a better way to handle this or can you point me to an article or reading that deals with this. Ive tried googling but a lot of the tutorials are very general and im finding it difficult to find something that deals with my specific issue. I also know about extend but it doesn't feel like an appropriate situation for extending the database class and i can still only extend one class i believe.</p>
  
  <p>The problem comes when i try and use:</p>
  
  <p>$errorHandling->database_connection();</p>
  
  <p>within the database class. If i do that i get an error:
  Call to a member function database_connection() on a non-object or something similar</p>