Home > php > PHP Configuration class

PHP Configuration class

December 9Hits:5
Advertisement

Created this little config class, any room for improvements? Think I have done an alright job but would like to know if any improovements could be made?

<?php defined("SECURE") or exit('Please define SECURE keyword to continue.'); class config {     private $configValues;     private $configFile;      public function __construct()     {         $configValues = array();     }      public function loadConfiguration($configFile = "C:\Users\ashle\Miracle\config\config.ini")     {         if (!file_exists($configFile))             loadDefault();          $content = file($configFile);         foreach ($content as $line)             $this->setConfig(explode("=", $line)[0], explode("=", $line)[1]);     }      private function loadDefault()     {         /* DEFAULT CONFIG SETTINGS!!            THIS SHOULD NOT BE THE CASE... */          setConfig("database.host", "localhost");         setConfig("database.username", "root");         setConfig("database.password", "");         setConfig("database.title", "mydb");         setConfig("database.port", "3306");     }      private function setConfig($name, $value)     {         $this->configVlaues[$name] = $value;     }      public function getConfig($name)     {         return $this->configValues[$name];     } } ?> 

Answers

Things that just pop up in my mind when seeing the code (more of the architecture and robustness rather than language constructs):

1) Harcoding path to ini file

The path might have a meaning in your development environment, but it will mean nothing in a test or production environment. I would set it in an included file and use it from there. Something like:

$defaultConfigFilePath = "C:\Users\ashle\Miracle\config\config.ini";

in loadConfiguration $configFile = !isset($configFile) ? $defaultConfigFilePath : $configFile;

Hopefully, they will introduce a coalesce operator soon to simply write:

$configFile = $configFile ?? $defaultConfigFilePath;

2) Error handling

loadConfiguration function does not handle any incorrect formatting. Any line which is not in the format name=value will not be imported correctly.

You can explode into an $lineArray and check if elements are defined and exactly two.

3) A more powerful config reader

This is a good exercise, but ini files have a more complex format that seems to be handled by parse_ini_file function.

Thanks for your answer, one thing that popped into my head to ask was what is the speed on something like this like? I mean would it be faster to just make a file and havea variable for each config item? Also security.. is this more secure locating the config file outside the website folder?

Related Articles

  • Is configuration with arrays a bad smell?February 20

    Something I see fairly often in PHP, where OOP / classes are concerned; $options = array ( 'name' => 'Harry Fuecks', 'favoritecolors' => array ('red','green','blue'), 'favoritefoods' => array ( 'breakfast' => 'English Breakfast', 'lunch' =>

  • Configure Web Logs in Apache

    Configure Web Logs in ApacheFebruary 23

    One of the many pieces of the Website puzzle is Web logs. Traffic analysis is central to most Websites, and the key to getting the most out of your traffic analysis revolves around how you configure your Web logs. Apache is one of the most - if not t

  • Configure Firewalls with a GUIMay 8

    I happened across this product a few days ago and have been evaluating it as a potential time-saver and also to streamline firewalls across multiple servers. Firewall Builder is a front end for configuring several firewalls, including iptables, ipfil

  • Install And Configure ColdFusion MX 6.1 on Windows

    Install And Configure ColdFusion MX 6.1 on WindowsSeptember 21

    I've made the case for ColdFusion, but you still need to know how to install it! In this tutorial, I'll show you how to install ColdFusion 6.1 on a Windows machine, and walk you through the basic set up process. If you're not running Windows, don't w

  • Using JSON for Language-independent Configuration FilesAugust 21

    The growing availability of JSON parsing in server-side frameworks elevates the usefulness of JavaScript beyond client-side programming, to providing base syntax for a generalized data interchange format. Well duh. But a not-immediately-obvious advan

  • How can I configure unattended installation of Ubuntu?April 30

    I'd like to configure an installer for Ubuntu which runs in 'unattended' mode and answers all the questions with defaults or pre-configured settings. Ideally it should run and reboot on it's own, resulting in a working system with no user input. Redh

  • Best single-server configuration for small Windows domain with VPNApril 30

    I work in a tiny two-person software company. The other employee is literally half a world away. I'd like to set up our single Windows server to provide all the services we'll need to run our domain. Here's what we have configured so far: Dell PowerE

  • Should I run my database off of a RAID 5 configuration?April 30

    I've heard that the write performance of RAID 5 can be appalling at times. While I want the redundancy that it provides I don't want to sacrifice my database insert/update times. Is this something I should be worried about and if so, what would be th

  • How do you configure sendmail to reject Email with a Date header too far from real time?April 30

    A friend of mine using (I believe) qmail has it configured to reject, as SPAM, any incoming Email with a Date: header that is too far from real time. I can't figure out how to do this with sendmail. How do you do this? --------------Solutions--------

  • Configure Outlook not to hide attachmentsApril 30

    How to configure Outlook to display all (incl. unsafe) mail attachments? --------------Solutions------------- Microsoft describe the solution in this KB article and lists all blocked attachments here. As there is no way to say "unblock all" you

  • Configure Wi-Fi adapters in LinuxMay 1

    Linux distributions these days come with NetworkManager to allow a user to configure a WiFi network adapter. However I can't seem to figure out how to configure a system-wide network adapter. There seems to be some suggestion that NetworkManager can

  • How can you configure windows to Auto Logon with specific username and password?May 1

    There are some scenarios for which you want to configure a machine to automatically login using a specific user account and password without blocking on the logon screen. How does one do that? --------------Solutions------------- Windows XP autologin

  • How to configure an external CentOS server to act as an HTTP Proxy/Relay?May 2

    I have an externally hosted CentOS 5 server (external.example.com) that I want to use as a relay/proxy for my local web requests. So basically, if I load google.com on my local computer I'd like for the request to appear to Google as if it was coming

  • Configuring Apache content negotiation for languages using MultiViewsMay 2

    I'm attempting to get the MultiViews option working in Apache to vary the content returned to the browser based on the Accept-Language provided in requests. I've got the following configuration: Alias /multiviewstest "C:/MultiViews Test" <Dir

  • How do I configure a windows domain controller to use an external time server?May 3

    I found this KB artice: http://support.microsoft.com/kb/816042 But it involves a bunch of manual registry hacks. I was hoping to find something buried in Administrative Tools somewhere that allowed me to do this. I'm using Windows Server 2003. ------

  • How do I configure SMTP when using Windows Server 2003 as a mail server?May 4

    I added the "Mail Server" role to my Windows Server 2003 server, which gave me POP3 and SMTP (I really only need SMTP). I need a server on my LAN that clients can use to email out without SSL or a login (I will NOT be allowing any traffic from t

  • How to configure Nagios alerts for Exchange Mail StoresMay 4

    Does anybody know how to configure Nagios to monitor MS Exchange mail stores? We have had mailstores unmount (because of diskspace, and other causes), and would like to monitor their state (mounted or not) via nagios. We use pNSClient.exe on our wind

  • How do I (or is it possible) to configure SSL Certificates for ADAM to self-renewMay 4

    I have an environment of Windows Server 2003 machines that are running ADAM (Active Directory Application Mode) over SSL. The SSL certificates that are installed on the machines are created using our internal CA and are trusted on any computer on our

  • How do I configure SPF for multiple domains on a server? (also allowing gmail as a sender)May 4

    SPF (Sender Policy Framework) seems like a good way to combat spammers/spoofing. However, despite reading the explanations several times, I'm not quite understanding how to configure it correctly. Let's say I have my server at a.x.com which hosts www

  • How can I configure Apache to use HTTPS for external access but HTTP for internal access?May 5

    I have two servers, one a development server that is accessible internally on our company's local network and the other a public-facing web server. The development server hosts several tools that we use to manage our projects internally but now we ne

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.310 s.