tags:

views:

76

answers:

1

I want to make a program that pulls objects from a sql database and processes the objects against a set of functions before returning the data to the user.

It will work like this:

  1. User specifies a date range in a javascript form.
  2. The date range is sent to a php file on the server that converts it to a sql query.
  3. The sql query pulls all data in the database that matches the date range, and stores each matching item and its associated information as a php class in a temporary php file (see example below).
  4. The main php program then processes each class in the temporary php file against a set of functions and returns the information to the user.

Example temporary php file:

class ice_cream
{
       $temperature = 0;
       $texture = 'soft';
}

class hard_candy
{
    $temperature = 20;
    texture = 'hard';
}

Example of Functions to process the classes against:

function will_it_break_dentures($class_name)
{
    //$class_name is passed to the function based on the classes available in temp file
    $food_type_class = new $class_name();
    $damage_potential_temperature = $food_type_class->temperature;
    $damage_potential_temperature = $food_type_class->texture;

    if($damage_potential_temperature >= 15) && ($damage_potential_texture === 'hard')
    {
        print("Don't eat it");
    }

}

Is there a more efficient/secure way to pull data from a database and process it against a set of functions? Is this the standard way to pull data from a database for processing? Item #3 seems suspect, but it's the best I could come up with. Any relevant informational resources on best practice are appreciated.

+2  A: 

Your class definition should define the properties of the object, not the values. It should be defined statically, not dynamically at runtime. At runtime you create instances of this class and populate the data for each instance.

Try something like this:

class Food {
   var $name;
   var $temp;
   var $texture;

   // Constructor
   public function food($name, $temp, $texture) {
      $this->name = $name;
      $this->temp = $temp;
      $this->texture = $texture;
   }

   public function breaksDentures() {
      if($this->temp >= 15) && ($this->texture === 'hard')
         return true;
      else
         return false;
   }
}

And use it like this:

function processFoods($daterange) {
   $query = build_query_from_daterange($daterange);
   $result = mysql_query($query);

   while($row = mysql_fetch_assoc($result)) {
      $food = new Food($row['name'], $row['temp'], $row['texture']);
      // Now $food is an instance of the Food class populate with the values
      // from the db, e.g., 'ice cream', 0, 'hard'.

      if $food->breaksDentures() {
         print("Don't eat it");
      }
   }
}

P.S. You may want to brush up on your object-oriented concepts. Your question is a good one but indicates some confusion about OO basics.

BenV
Thanks for recognizing my confusion and providing detailed examples. This has me started down the correct path.
JMC