PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Feb 24, 2020 10:52 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Validating objects?
PostPosted: Thu Nov 10, 2011 4:51 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
I'm trying to create an invoice class which is heavily dependent upon data from our DB. I've defined some class properties -- account number, invoice number, invoice date, and invoice details -- upon which most of the class methods will rely. The problem, or perhaps one of the problems, is that this data is scattered across numerous tables. Currently, the constructor requires only a single argument: invoice number. If this number does not correspond to a valid invoice number (ie. we have no such invoice) then most of the class methods will fail, including the methods required to assign values to the class properties. These, in turn, also need to point to valid database records.

I'm not really sure how to proceed from here. Does calling all the property setter methods (ie. $this->account_number = $this->getAccountNumber()) in the constructor and throwing exceptions if any of them returns FALSE make sense? Am I thinking about this completely wrong?

_________________


Top
 Profile  
 
 Post subject: Re: Validating objects?
PostPosted: Thu Nov 10, 2011 7:08 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
You can throw exceptions, or have some sort of convention for invalid return values (e.g. null), or you could implement a isValid() method that can be called before code section that use this object to verify that it was initialized properly. It really depends.

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: Validating objects?
PostPosted: Thu Nov 10, 2011 7:17 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
I had thought about having the various class methods return FALSE on failure and potentially having that bubble up as needed (ie. if method A calls method B and method B returns FALSE, then method A will also return FALSE). Seemed like an awful lot of repetition, though. I'm intrigued by your isValid() suggestion. How exactly did you mean? Instantiate the object using whatever number the constructor is provided, then have some class method check the database for a match which, in turn, would set a $_isValid property to either TRUE or FALSE?

_________________


Top
 Profile  
 
 Post subject: Re: Validating objects?
PostPosted: Fri Nov 11, 2011 1:39 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
Yeah ... it seem like by the end of the constructor running you should know whether the object is initialized properly.

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group