SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict thoresson's Avatar
    Join Date
    Dec 2002
    Location
    Gothenburg, Sweden
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    configuration class - skeleton code for first OOP adventure

    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 skeleton a good start, or should I change anything?

    PHP Code:
    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->configurationFileTRUE);
        return 
    $configurationArray;
      }

      function 
    writeConfigurationFile($changedValues)
      {
        
    $fp fopen($this->configurationFile"r");//open file
        
    $contents fread($fpfilesize($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

  2. #2
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would capitalize the class neame as well as the constructor name "Configuration()" That seems to be prety standard.

    You can clean things up by shortening some functions.

    PHP Code:
    function readConfigurationFile() 
      { 
        Return 
    parse_ini_file($this->configurationFileTRUE); 
      } 
    And

    PHP Code:

      $new_contents 
    ereg_replace($changedValue[old], $changedValue[new], $contents); 
          
    $contents $new_contents
    Could be
    PHP Code:
     $contents str_replace($changedValue[old], $changedValue[new], $contents); 

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since your name of the class gives the class and it's purpose right away I'd make some more changes yes ?

    For example in all your class methods, remove

    PHP Code:
    *ConfigurationFile ... 
    So for example...

    PHP Code:
    function readConfigurationFile() 
      { 
        
    $configurationArray parse_ini_file($this->configurationFileTRUE); 
        return 
    $configurationArray
      } 
    Would simple be

    PHP Code:
    function read() // or access()
      

        
    $array parse_ini_file($this->GetFile(), TRUE); 
        return 
    $array
      } 
    Notice also I've put a Getter in there to fetch the file yes ?

    Any more questions I can easilly help if I can

  4. #4
    SitePoint Addict thoresson's Avatar
    Join Date
    Dec 2002
    Location
    Gothenburg, Sweden
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    PHP Code:
    function read() // or access()
      

        
    $array parse_ini_file($this->GetFile(), TRUE); 
        return 
    $array
      } 
    Notice also I've put a Getter in there to fetch the file yes ?
    What does $this->GetFile() do that my original $this->configurationFile didn't?

  5. #5
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by thoresson
    What does $this->GetFile() do that my original $this->configurationFile didn't?
    The doc might have a good reason, but I'm guessing that it's just a matter of taste.

    --ed

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally I like to use Getters and Setters;

    Thoresson; In your original script you accessed the name of file as is; ie by the class property yes ?

    Well, at a later date for example, you change the name of this variable yes ? And you access this variable in a dozen places within your class...

    Which would mean you'd need to look through your class to change the variable 12 times

    I put a Getter in there so you could add the required

    PHP Code:
    ...
    function 
    GetFile() {
    // return the class property holding filename
    }
    ... 
    class method yourself; If you're using a Getter you only need to make a change to your class in one place.

    Hope this makes some sense ?

    But to the matter if you actually should use Getters and Setters, this is down to yourself; There is nothing written in stone as far as OOP goes to say you've got to use them

  7. #7
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm. Would that not make the code a little bit inflated if you use a large class with many different variables?

    Also, it's nice to have a php editor with search and replace capabilities.

  8. #8
    SitePoint Addict thoresson's Avatar
    Join Date
    Dec 2002
    Location
    Gothenburg, Sweden
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After your help, I now have a slightly remade class:

    PHP Code:
    class Configuration 
        

        var 
    $configurationFile
        
        function 
    Configuration($configFile
        { 
            
    $this->choose($configFile); 
        } 
        
        function 
    choose($configFile
        { 
            
    $this->configFile $configFile
        }
        
        function 
    getFile()
        {
            Return 
    $this->configFile;
        }
        
        
    // Working methods start here
        
        
    function read() 
        { 
            Return 
    parse_ini_file($this->getFile(), TRUE); 
        } 
        
        function 
    write($changedConfigValues
        { 
            
    $fp fopen($this->getFile(), "r" );//open file 
            
    $contents fread($fpfilesize($this->getFile())); 
            
    fclose($fp); 
        
            foreach (
    $changedConfigValues as $oldConfigValue => $newConfigValue
            { 
                
    $contents str_replace("$oldConfigValue""$newConfigValue"$contents);
            } 
        
            
    $fp fopen($this->getFile(), "w" ); 
            
    fwrite($fp$contents); 
            
    fclose($fp); 
        } 

    I use it this way:

    PHP Code:
    <?php

    include ("configuration.class.php");
    $config = new Configuration("settings.ini");
    $settings $config->read();
    ?>
    <form method="post" action="configupdate.php">
    <?php
    foreach ($settings as $setting => $values)
    {
        
    ?>
        <b><?php echo $setting?></b>
        <br>
        <?php foreach ($values as $setting => $value)
        {
            echo 
    $setting;
            
    ?>
            <input name="newValues[]" type="text" value="<?php echo $value?>">
            <input name="oldValues[]" type="hidden" value="<?php echo $value?>">
            <input name="settings[]" type="hidden" value="<?php echo $setting?>">
            <br>
            <?php 
        
    }
    }
    ?>
    <input type="submit">
    </form>
    And on the "recieving" end:
    PHP Code:
    <?php

    include ("configuration.class.php");
    $config = new Configuration("settings.ini");
    $repeat count ($_POST['newValues']);
    $settings $_POST['settings'];
    $oldValues $_POST['oldValues'];
    $newValues $_POST['newValues'];
    --
    $repeat;
    for (
    $i 0$i <= $repeat$i++)
    {
        if (
    $oldValues[$i] != $newValues[$i])
        {
            
    $arrayName $settings[$i] . " = " $oldValues[$i];
            
    $arrayValue $settings[$i] . " = " $newValues[$i];
            
    $changedValues[$arrayName] = $arrayValue;
        }
    }

    $config -> write($changedValues);
            
    ?>
    Changes saved!
    Any obvious mistakes?

    //Anders

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would that not make the code a little bit inflated if you use a large class with many different variables?
    Not if you've designed the class well ? The whole point of OOP is that you encapsulate one very specific task within a class and nothing more; No frills and spills folks

    If you have a class and you think for one minute that it is going to be bloated then break it in down again;

    Software Layering and encapsulation is never easy though; A concept I have issue's with myself still

    But that is what you've got to do, then it's got to be done as I see it.

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any obvious mistakes?
    Umm.... Looking briefly it looks clean enough and it does it's purpose well yes ?

    Not bad actually


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •