NYCPHP Meetup

NYPHP.org

[nycphp-talk] Object Oriented Programming question

Hans Zaunere zaunere at yahoo.com
Wed Jun 19 14:45:38 EDT 2002


--- Brent Baisley <brent at landover.com> wrote:
> I've setup a "settings" file that I would like to contain "default"
> values like the mysql login name, password and database.

I of course don't know your complete situation, but I would do the
inverse: a settings file contains the normal values, and default
settings are hardcoded.

> One of the functions in my class file is a db connection function. 
> I've set it up to accept the login name, password and database as
> parameters that can be passed.
> 
> What I would like to do is use default values set in the settings
> file if parameters aren't passed.

OK, if you say so :)

class SettingsFile
{
  var $DB_User              = NULL;
  var $DB_Password          = NULL;
  var $DB_DB                = NULL;

  function HandleSettingsFile() {
    $filehandle = fopen(blahblah);
    $this->DB_User = parseblahblah($filehandle);
    $this->DB_Password = parseblahblah($filehandle);
    $this->DB_DB = parseblahblah($filehandle);
  }
}

class MyDBStuff extends SettingsFile
{
  var $DB_Handle           = NULL;

  function DB_Connect( $user = NULL, $password = NULL, $db = NULL ) {
    if( $user !== NULL )
      $this->DB_User = $user;
    if( $password !== NULL )
      $this->DB_Password = $password;
    if( $db !== NULL )
      $this->DB_DB = $db;

    $dbhandle = 
      connectblah($this->DB_User,$this->DB_Password,$this->DB_DB);
  }
}

$dbstuff = new MyDBStuff;

if( $I_HAVE_A_SETTINGS_FILE === 1 ) {
  $dbstuff->HandleSettingsFile();
  $dbstuff->DB_Connect();
} else
  $dbstuff->DB_Connect('user','password','a_db');



> I tried using the syntax for setting a default
> value for a parameter if a value isn't passed, but I guess you can't
> use variables in this since I get errors. 

Right.. only constants work with that.

> I want to only pass values to the function if I don't want to use the
> default values.

The heart of my lousy example is the $this->DB_Connect method. 
Personally, I would avoid this all together, since if anyone is
including your code, they can see what your login/password/db name is. 
Instead, I would only NOT use default values if something from the
settings file was supplied.  There is a fine line here between the two,
but there is a different.  I always hardwire my password/login info at
the top include file; then if someone needs to supply something
different down the road, they can, yet they can never see what my
defaults are.


parseblahblah,

HZ



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



More information about the talk mailing list