SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,013
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)

    PHP Function of the Week - parse_ini_file

    I once tried to go over these once a day - that proved to be too much so I'm going to start doing these on Wednesdays or Thursdays. To keep things varied I wrote a script for this.

    Code php:
    <?php
    header("Content-Type: text/plain");
     
    $f = get_defined_functions();
    $f = $f['internal'];
     
    shuffle($f);
     
    echo array_pop($f);
     
    exit;

    What I'm going to do is each Wednesday I'll run this script and write something about the function it spits out at me. So, in a nod to the old Animaniacs cartoon let us begin...


    Array of PHP functions, shuffle and pop -
    Tell us the which function we'll learn this stop.


    parse_ini_file


    Cool, ok. This function parses ini files in a format similar to the php.ini file itself, but as of PHP 5.3 a newer version of the ini file format is supported that allows some degree of nesting. INI files are a great way of storing configuration data for your program, and I've personally used them extensively for that purpose. While it is true that YAML and XML files allow for more data depth and complexity than INI files, INI files have a strong advantage in being much faster to parse and, in my opinion, any data amount so extensive that a YAML or XML file would be required should probably reside in your database.

    Parsing ini files is one of the very first things my own framework does, as seen here in this function from its core bootstrap class.

    Code php:
    	/**
    	 * Load and parse the config.ini file of this project.
    	 * @param $config
    	 * @param $path
    	 */
    	private final static function loadConfig( $config ) {
     
    		self::$config = parse_ini_file($_SERVER['PNL_ROOT'].'core/config/core.ini', true);
     
    		if ( self::$config == false ) {
    			die('Parse error in configuration file. PNL cannot start.');
    		}
     
    		if (!is_null($config)) {
    			$childConfig = parse_ini_file( $config, true );
     
    			if ( $childConfig == false ) {
    				die('Parse error in supplied override configuration file. PNL cannot start.');
    			}
     
    			self::$config = array_merge_recursive( self::$config, $childConfig );
    		}
    	}

    As to what this function is doing - the framework has a master default INI file whose location relative to the root is known. The value of $_SERVER['PNL_ROOT'] is set in the httpd.conf or .htaccess file in this manner.

    Code:
    SetEnv PNL_ROOT "c:/xampp/pnl/"
    You can actually arbitrarily load any value into the $_SERVER array from your htaccess file, though these keys should be kept minimal: The path to the framework root where its files lie, and whether to start in debug mode, are the only ones I set. That gets us to the config file, which holds the values that must be known to the system before it tries to start a database. All other data goes in the database.

    PNL is designed to let one core code block serve a whole server regardless of how many vhosts it might have and clients with those hosts. Hence, the startup of the framework takes a path argument for a local config file. The function above loads that second and then uses array_merge_recursive to marry the two files together into a master config.

    So, what else is there to say or questions to ask regarding this function?

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Array of functions, shuffle and pop
    Tell us the function that you'll drop.

    It's probably worth mentioning what PNL-is-, rather than just dropping an acronym in there blind.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,013
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    PNL is a framework I've been working on for 3 years that never seems to get finished.

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Personally, I think it would have been more worthwhile to factor PNL out of the code example altogether. You ended up spending several paragraphs talking about PNL, yet only a couple sentences talking about the function that was supposed to be the topic of this post.

    Otherwise, I think these posts are an interesting idea. It's a nice way to discover something new and interesting, even if it's about a function you already thought you knew. For example, I didn't know that we could nest values.
    "First make it work. Then make it better."

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Seeing as nobody has actually shown an ini file yet .....

    I use parse ini to pick up files in a framework which define how PDO should behave in the model direction, and how the forms should be rendered in the view direction.

    staff.model.boot.php
    PHP Code:
    ;<?php exit(' you won\'t see my ini file'); ?>
    ; Bootstrap file for simple models
    ; Options for PDO are : INT STR NULL BOOL LOB
    ; params : PDO - field_type - size - HTML_Label

    [attributes]
    id             = INT
    type_ref    = INT
    name        = STR-text-30-Name
    title       = STR-text-30-Job title
    email       = STR-text-30-Email
    ext         = INT-text-30-Ext no:
    tel         = STR-text-30-Tel
    descrip     = STR
    rank        = INT-text-2-Order on page

    [second_section]
    ; left empty
    I name them .php rather than .ini so they is a slightly less chance they will be shown as plain text.

    Here's a (rather badly named) helper which grabs these ini files:

    iniconfig.class.php
    PHP Code:
    <?php

     
    /**
     * class IniConfig 
     * 
     * Bootstraps from an .ini file and provides basic crud for that single table
     * 
     **/

    class IniConfig {

    public 
    $ini_file;
    public 
    $config;

              function 
    __construct$model_name="" ){

              if( 
    $model_name=="" ) { trigger_error("No ini file requested"E_USER_ERROR);}

              
    $this->ini_file $model_name".model.boot.php" ;

                if( !
    $this->config parse_ini_file$model_name ".model.boot.php"true) ) {
                     
    trigger_error("Ini file does not exist : $model_name"E_USER_ERROR);
                }
              }

    // each ini section holds an array we need to tell it what to look for

    function setKeyTarget$key ){

              if ( 
    in_array$key array_keys($this->config ) ) ) {
              return 
    $this->config[$key] ;

              }else{
              
    trigger_error"No such settings $keyE_USER_ERROR );

              }

    }
    }
    ?>


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
  •