PHP: how to abort construction of an object?

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
i'm creating a couple classes to control user authentication and navigation in a website...

one class' __construct() receives 2 arguments as parameters, validates them and if they are both valid stores them and initializes the rest of the class. if validation fails, i made it return NULL in order to try and stop the object from being instantiated.

my logic is that if i can make sure the object is only instantiated if the arguments are valid then i can safely assume they will be throughout the object's life. this also makes it more efficient as I don't have to fully create the object if incorrect parameters are passed.

however, returning NULL (or false) from the __construct() function doesn't stop the object from being created, it merely skips the rest of the initialization and can still be used normally.

one way around this would be to validate the entries before attempting to create the object, but now i'm curious as to whether it's possible to tell a constructor to 'abort'


thanks! :)
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
you could put an exit; in the construct, but your object will still exist

you could have a 2nd object which is created by the first

sounds like you should have a paramValidate method in the class, and use it in your script for any logic

are you pusing php4 or 5?
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
php5

right now i'm validating entries in the same page as the form and if validation passes i create the object...

i googled for paramValidate but couldn't find anything helpful...
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
Originally posted by: alex
php5

right now i'm validating entries in the same page as the form and if validation passes i create the object...

i googled for paramValidate but couldn't find anything helpful...

sorry i should have been more clear, you should write a paramValidate method for the class :)
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
oh! :)

yeah i already have methods that do that, but it's pointless to have them inside the class because what I'm trying to avoid is creating a class if i get invalid information.

i moved the validation methods to a separate include and now in my form page i validate and if validation passes i create an instance of the class... so the class does no error checking of its own... not sure if that's good or bad but since this is a personal project i can probably get away with it... it's probably considered bad practice i guess to expect validation to be done outside of the class... i want to keep it easy to read though
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
my favorite thing about classes, is that all the related code is basically in one place.

so when the data handling changes, and the validation changes - all the code is in one sweet little organized file.
 

Alex

Diamond Member
Oct 26, 1999
6,995
0
0
Originally posted by: troytime
my favorite thing about classes, is that all the related code is basically in one place.

so when the data handling changes, and the validation changes - all the code is in one sweet little organized file.

now i have it in 2 sweet little files :)

one just for validation and one just for data handling

i'm trying to keep the classes as vague as possible so that i can possibly re-use them in future classes