PHP code example of kayzorelabs / phpconfigparser

1. Go to this page and download the library: Download kayzorelabs/phpconfigparser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kayzorelabs / phpconfigparser example snippets

bash
$ php composer.phar update kayzorelabs/phpconfigparser


    namespace Your\Namespace;

    use KayzoreLabs\ToolKit\ConfigParser\ConfigParser;

    $cfg = new ConfigParser();

    // load file
    $cfg->read('/home/user/.config/server.cfg.sample');

    // modify a value (section, option, value)
    $cfg->set('github.com', 'user', 'bar');

    // and save it
    $cfg->save();

    // ... or, write to another file
    $cfg->write('/home/user/.config/server.cfg');

    


    namespace Your\Namespace;

    use KayzoreLabs\ToolKit\ConfigParser\NoSectionsConfigParser;

    $cfg = NoSectionsConfigParser();

    $cfg->read('/tmp/sectionless.cfg');

    $cfg->set('server', '192.168.1.1.');

    echo $cfg->get('server');

    


    namespace Your\Namespace;

    use KayzoreLabs\ToolKit\ConfigParser;
    use Monolog\Logger;
    use Monolog\Handler\StreamHandler;

    class MyConfigParser extends ConfigParser
    {
        protected $logger;

        public function __construct(array $defaults = array(), array $settings = array())
        {
            parent::__construct($defaults, $settings);

            // create a log channel
            $this->logger = new Logger('ConfigParser');
            $this->logger->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
        }

        public function log($message)
        {
            // add records to the log
            $this->logger->addError($message);
        }
    }