NYCPHP Meetup

NYPHP.org

[nycphp-talk] Object Oriented Programming answer

charles at softwareprototypes.com charles at softwareprototypes.com
Tue Jun 18 16:48:36 EDT 2002


Hello Brent,

the object instance you are creating with your "settings file" is an
Environment object and I would create an Environment Class and a
method to fill it from that file (or from another source, [now you
could use that method to connect the environment variable over a
socket to your local machine... lets say] :-) and other methods to to
set/get a DBConnection instance variable to permit any other method
in any class to use the connection.

Environment class
  class method
    new -> answers a new initialized instance of the Environment
      get used to thinking of instantiation as occurring in context
and
      the code having to validate the context before answering
      a) raising an error/exception and answer nil
      b) the instance
  instance variables 
    dbUser, 
    dbPasswd, 
    dbName,
    <whatever else you need from your environment> ...
  instance method
    load ($stream) -> sets values from source stream
      (file, socket, parm, lottery ticket, ESP, whatever.)
    loadFile ($fileName) -> open the file named fileName & call
      load($source) with $source set to whatever you're connected to.
    <instVar>($value) -> sets and answers the value of the instvar
    <instVar>() -> gets the value of the instVar

Say  your instVars are dbUser, dbPwd, dbName

You should instantiate the Environment Class at the beginning of your
session or run-unit or unit_of_work (there's a dozern aliases for
this.)

 $env = Environmenr new; /* check for an error */
 $env loadFile($yourFileNameOrALitteral);

 ...

  function db_connect(
    $db_user   = $env.dbUser(), 
    $db_pswd   = $env.dbPwd(), 
    $db_dbname = $env.dbName() );

 ...

In addition, you could write your Environment.<instVar> methods to
use lazy evaluation and, if there in no value for <instVar> already
set, say from reconnecting to a session, you could then call
loadFile() from within the Environment.<instVar>() method, uh,
OOP-speek, sorry, function.

The idea is that you now have an single object which has the
responsability for getting and setting all environment valueand the
rest of your code doesn't have to worry about or care if it gets the
vaues from a default, a file, a session or if it pulls off a Kresgin
and reads the guy's inseam.





More information about the talk mailing list