alexweber15 Posted September 9, 2008 Share Posted September 9, 2008 In a nutshell this is the interaction sequence: (doubts are in red) User posts html form Form is validated If validation passes: Clean Up data ? Instantiate Class representing a filled form (ex: User Profile) Newly UserProfile object sticks around for duration of session AND passes itself to "StoragePrep" Class, which in turn prepares the data for storage (XML, SQL, etc) and finally passes itself to the corresponding class that will perform the CRUD operations - is data cleanup really necessary at this point? (assuming strict validation?) - is a class passing itself as an argument to the next class' constructor (ie UserProfile to StoragePrep) a good practice considering it might still be active if the user does something else (reference issues here) - can any of these afforementioned classes be static? (for example StoragePrep and Validator specifically) BTW: The project has no inheritance and relies on Type Hinting using interfaces to make sure the correct parameters get passed back and forth thanks! im trying to makes this as loosely coupled as possible and the interface over inheritance really helps, but i think i might be instantiating too many unecessary classes.... any feedback, thoughts, tips, criticism, cookies or beer appreciated! thanks! -Alex Quote Link to comment Share on other sites More sharing options...
keeB Posted September 9, 2008 Share Posted September 9, 2008 If validation passes: * Clean Up data ? * Instantiate Class representing a filled form (ex: User Profile) * Newly UserProfile object sticks around for duration of session AND passes itself to "StoragePrep" Class, which in turn prepares the data for storage (XML, SQL, etc) and finally passes itself to the corresponding class that will perform the CRUD operations You should be passing a Model object to your Persistence layer. How your 'StoragePrep' knows what to do with said Model object is up for debate. You can use the PHP Reflection API, but most frameworks use XML or YAML as a mapper. Example of a model object <?php interface Entity { function returnSetList(); function returnGetList(); } class Person implements Entity { private $firstName; private $lastName; public function setFirstName($name) { $this->firstName = $name; } public function setLastName($last) { $this->lastName = $last; } public function getFirstName() { return $this->firstName; } public function getLastName() { return $this->lastName; } public function returnSetList() { return array("setFirstName", "setLastName"); } public function returnGetList() { return array("getFirstName", "getLastName"); } } ?> Normally, there are helpers that exist for model objects known as DAO's. You can think of these as factory's of Model objects. Here's an example: <?php class BaseDao { public function saveEntity( Entity $entity) { $func_list = $entity->returnGetList(); $list = array(); // hold the values in an array (this is dumb but here for reference) foreach ($func_list as $function) { $list[] = call_user_func($entity, $function); } //inspect the list and do something with it. } } class PersonDao extends BaseDao { public function savePerson(Person $person) { if ($person != null) saveEntity($person); } } ?> I typed that on a whim, but that is how I would do it via Reflection (or something similar.) Quote Link to comment Share on other sites More sharing options...
keeB Posted September 9, 2008 Share Posted September 9, 2008 Here are some links: http://en.wikipedia.org/wiki/Data_Access_Object http://en.wikipedia.org/wiki/Object-relational_mapping Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted September 9, 2008 Author Share Posted September 9, 2008 thanks! i talked to a Prof at uni today and he showed me a "simple" PHP/MySQL CMS all the way from the UML Case of Use, Activity Sequeces and Class diagrams and the parts about Persistence and DAO were really helpful... very reassuring that you guys agree what about the Validator though??? is having a static validator a bad idea? (i forget whether static classes can have private attributes but i doubt it) if not, where in the whole process would i implement validation? (in the HELPER?) thx!!! Quote Link to comment Share on other sites More sharing options...
keeB Posted September 10, 2008 Share Posted September 10, 2008 I would assume the set methods in your object would be sanitized on Input if coming from a Form. In cases where this cannot be guaranteed (you better have a good reason) enforce the validation in the actual Set method itself. Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted September 11, 2008 Author Share Posted September 11, 2008 thanks KeeB! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.