PHP Exceptions in Classes
Posted
by mike condiff
on Stack Overflow
See other posts from Stack Overflow
or by mike condiff
Published on 2010-04-21T19:13:01Z
Indexed on
2010/04/21
19:23 UTC
Read the original article
Hit count: 257
php
|exceptions
I'm writing a web application (PHP) for my friend and have decided to use my limited OOP training from Java.
My question is what is the best way to note in my class/application that specific critical things failed without actually breaking my page.
Currently 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 querystring that does not exist, I pass it to my objects constructor and the load fails. Currently I don't see a way for me to just return false from the constructor.
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.
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.
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 osrt of design pattern that I should look into?
Any help would be appreciated.
© Stack Overflow or respective owner