configuration class - skeleton code for first OOP adventure

From: Date: Wed, 08 Oct 2003 17:52:21 +0000
Subject: configuration class - skeleton code for first OOP adventure
Groups: php.general 
Request: Send a blank email to php-general+get-165486@lists.php.net to get a copy of this message
Hi, My first larger project is growing out of control. I've spent some weeks reading OOP tutorials, and feel ready to make my first dive into a new programming style. One of the things that led me this way was the need for user configuration of my project. Therefor, I'll start with a class that let's me read and write a configuration file. Is this a good start, or should I change anything? class configuration { var $configurationFile; function configuration($configurationFile) {
    $this->setConfigurationFile($configurationFile);
} function setConfigurationFile($configurationFile) {
    // Code to check that $configurationFile points to a valid file
} function readConfigurationFile() {
    $configurationArray = parse_ini_file($this->configurationFile, TRUE);
    return $configurationArray;
} function writeConfigurationFile($changedValues) {
    $fp = fopen($this->configurationFile, "r" );
    $contents = fread($fp, filesize($this->configurationFile));
    fclose($fp);
    foreach ($changedValues as $changedValue)
    {
      $new_contents = ereg_replace($changedValue[old], $changedValue[new], $contents);
      $contents = $new_contents;
    }
    $fp = fopen($this->configurationFile, "w" );
    fwrite($fp, $contents);
    fclose($fp);
} } Best regards, -- anders thoresson

« previous php.general (#165486) next »