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.

JMC
  • 1,700
  • 7
  • 25
  • 34
  • 2
    `stores each object as a php class in a temporary php file` - say what? whats that supposed to mean? – Mchl Aug 07 '10 at 22:46
  • I like that classes can hold a set of variables and associate it with a class name. The database would store php classes. The sql query would pull the classes(i'm calling them objects) from the database and create a temporary php file that the main php program could process against. I might be going at this all wrong. – JMC Aug 07 '10 at 22:54
  • Yeah you do... First of all class != object. And if you want to fetch objects from database, then mysql extension offers you mysql_fetch_object() function. Personally I'm not really a fan of this approach, but well... – Mchl Aug 07 '10 at 23:07
  • A concrete example might help to clarify this whole class/object thingy. Exactly what would be stored in these temporary php files and what would the "main" script do with them? – VolkerK Aug 07 '10 at 23:18
  • Ok, so you really want to put a class declaration/definition into those files. Why? Can't `class ice_cream` be defined somewhere else (more or less permanently) and the result of the query be an _instance_ of that class? (And exactly what is the main script supposed to do with the class in the temporary file in your example?) – VolkerK Aug 07 '10 at 23:32
  • @VolkerK: I added the rest of the stuff you asked for in a new edit. Actually saved the first edit before I read your question. The main issue is that I can't permanently define any of the data in the database. My reason for turning the data into classes is so that I can process it against my functions. I'm wondering if this is a good approach or a bad habit. – JMC Aug 07 '10 at 23:43
  • Given your example I still can't see why you have to define classes for this. What would be the (significant) advantage of having `class ice_cream { ... } class hard_candy { ... }` over e.g. `array( 'ice_cream'=>array(...), 'hard_candy'=>array(...))`? Having _x_ classes with the identical structure and each instance having the same state ...that needs a lot of explaining to make any sense, sorry. – VolkerK Aug 08 '10 at 00:10

1 Answers1

2

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
  • 12,052
  • 13
  • 64
  • 92
  • Thanks for recognizing my confusion and providing detailed examples. This has me started down the correct path. – JMC Aug 08 '10 at 19:33