Jump to content

[SOLVED] When to use static classes and how to exchange information more efficiently...


alexweber15

Recommended Posts

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

 

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.)

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!!!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.