title: PHP Exceptions in Classes
body: <p>I'm writing a web application (PHP) for my friend and have decided to use my limited OOP training from Java.</p>
  
  <p>My question is what is the best way to note in my class/application that specific critical things failed without actually breaking my page.</p>
  
  <p>My problem is I have an Object "SummerCamper" which takes a camper_id as it's argument to load all of the necessary data into the object from the database. Say someone specifies a camper_id in the query string that does not exist, I pass it to my objects constructor and the load fails. I don't currently see a way for me to just return false from the constructor.</p>
  
  <p>I have read I could possibly do this with Exceptions, throwing an exception if no records are found in the database or if some sort of validation fails on input of the camper_id from the application etc.</p>
  
  <p>However, I have not really found a great way to alert my program that the Object Load has failed. I tried returning false from within the CATCH but the Object still persists in my php page. I do understand I could put a variable $is_valid = false if the load fails and then check the Object using a get method but I think there may be better ways.</p>
  
  <p>What is the best way of achieving the essential termination of an object if a load fails? Should I load data into the object from outside the constructor? Is there some sort of design pattern that I should look into?</p>
  
  <p>Any help would be appreciated.</p>
  
  <pre><code>function __construct($camper_id){
          try{
              $query = "SELECT * FROM campers WHERE camper_id = $camper_id";
              $getResults = mysql_query($query);
  
              $records = mysql_num_rows($getResults);
  
              if ($records != 1) {
                  throw new Exception('Camper ID not Found.');
              }
  
              while($row = mysql_fetch_array($getResults))
              {
                  $this-&gt;camper_id = $row['camper_id'];
                  $this-&gt;first_name = $row['first_name'];
                  $this-&gt;last_name = $row['last_name'];
                  $this-&gt;grade = $row['grade'];
                  $this-&gt;camper_age = $row['camper_age'];
                  $this-&gt;camper_gender = $row['gender'];
                  $this-&gt;return_camper = $row['return_camper'];
              }
          }
          catch(Exception $e){
              return false;
          }
  
  
  
      }
  </code></pre>