NYCPHP Meetup

[nycphp-talk] Object Oriented Question

Hans Zaunere zaunere at yahoo.com
Sun Jun 9 14:38:28 EDT 2002


> Currently I'm just doing this with a single SQL query (simple join of
> the two tables) and echoing out the results.
> Very fast.

The way to go.

> 
> I'm trying to envision how to create this page using object oriented
> code.
> My initial thought was to somehow use a game object, but if I have to
> instantiate a game object for every game, this seems like it would
> have
> an enormous amount of overhead and be slow.
> 
> How is this usually done on OO projects ?

I don't know how to do this in conformance with true OO, but I run into
this type of problem a lot (ie, looking at the medical history of an
individual, joined from multiple tables and disparat databases). 
Basically, the best way I've found to handle this, is using
PopulateAll(), PopulateByID(), etc type methods.  Then when the
database returns multiple rows for a single query, I make the
properties into arrays, where each element is a row from the DB.

> 
> Am I missing something or would you say that if your data sits in a
> relational
> database and your doing a search or mass listing of data then OO code
> is not the way to go.

I think OO is generally a good idea, however you need to be careful not
to over do it.  My basic premise is to use objects as small, self
contained programs (taking advantage of the "global" scope within
them).  Otherwise, I've spent hours trying to figure how everything
should fit together, and then realizing that PHP isn't anywhere near a
full OO language.

I really would like to see some kind of multiple inheritance (or
interfaces as Java calls them I believe), because without, I find that
OOP becomes very limited.  In an effort to get around this in PHP, I've
done what is probably a very bad thing.

class ImStatic
{
  function external_functionality() {
     $this->Result = mysql_query("SELECT * FROM sometable", $this->DB);
  }
}


class MyRealClass
{
  var $DB;
  var $Result;

  function MyRealClass() {
     $this->DB = connect_up_database();
  }

  function some_external_functionality() {
    ImStatic::external_functionality();
  }
}


$myrealobj = new MyRealClass();
$myrealobj->some_external_functionality();

And bam, $myrealobj->Result is filled it.  Now, admitingly, there is
something wrong feeling about this, however it comes in handy.  What
I've been doing lately, is building a master class, which is primarily
properties and some wrapper functions.  Then, I have a couple other
classes, that are static, and only contain functions, which end up
operating on the master classes' properties.  It's a little backwards
and awkward, but so far I haven't gotten burned... yet.

Anyway, I would not create a new object for every game/review.  Use
arrays (either internally to an object, or externally).

HZ




> 
> comments/suggestions/thoughts/ideas/etc ?
> 
> Thanx
> Ophir 
> 
>  
> ----
> Ophir Prusak
> Internet developer 
> prutwo at onebox.com | http://www.prusak.com/ 
> 
>  
> 


__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com



More information about the talk mailing list