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

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.