Results 1 to 1 of 1
Hybrid View
-
9th Dec 2010, 09:59 PM #1OPMemberWebsite's:
litewarez.net litewarez.com triniwarez.comCreating a simple Object Config system in PHP
Creating a config system that's suitable for any website application, this is the way 1 of the ways I use.
Why?
- It has a global scope
- You can enable / disable changes to protected configs
- You can add any settings during anywhere within runtime.
- You can make the class automated to fetch public configs from a file / DB.
Example:
PHP Code:class Settings
{
static private $protected = array(); //For DB / Passwords etc
static private $public = array(); //For all public strings such as meta stuff for site
public static function getProtected($key)
{
return isset(self::$protected[$key]) ? self::$protected[$key] : false;
}
public static function getPublic($key)
{
return isset(self::$public[$key]) ? self::$public[$key] : false;
}
public static function setProtected($key,$value)
{
self::$protected[$key] = $value;
}
public static function setPublic($key,$value)
{
self::$public[$key] = $value;
}
public function __get($key)
{//$this->key // returns public->key
return isset(self::$public[$key]) ? self::$public[$key] : false;
}
public function __isset($key)
{
return isset(self::$public[$key]);
}
}
PHP Code:<?php
Settings::setProtected('db_hostname','localhost');
Settings::setProtected('db_username','root');
Settings::setProtected('db_password','');
Settings::setProtected('db_database','root');
Settings::setProtected('db_charset','UTF-8');
//...
echo Settings::getProtected('db_hostname'); //localhost
//...
Settings::setPublic('config_site_title','MySiteTitle');
Settings::setPublic('config_site_charset','UTF-8');
Settings::setPublic('config_site_root','http://localhost/dev/');
?>
PHP Code:$Template = new Template();
$Template->Assign('settings',new Settings());
Code:<html> <head> <?php echo isset($settings->config_site_title) ? $settings->config_site_title : 'Fallback Title'?> </head> </html>
This can get a lot more complex but more system friendly, some examples:
- A LoadConfig method to automatically parse a config file, xml,php,yaml
- If you register an shutdown_function you can auto update the DB with new settings.
- You can auto populated the class with config from that database
- You can implement iterator's to make it compatible with looping
- Lots more.
litewarez Reviewed by litewarez on . Creating a simple Object Config system in PHP Creating a config system that's suitable for any website application, this is the way 1 of the ways I use. Why? It has a global scope You can enable / disable changes to protected configs You can add any settings during anywhere within runtime. You can make the class automated to fetch public configs from a file / DB. Rating: 5Join Litewarez.net today and become apart of the community.
Unique | Clean | Advanced (All with you in mind)
Downloads | Webmasters
Notifications,Forum,Chat,Community all at Litewarez Webmasters
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Creating a simple, safe contact form
By Areon in forum Server ManagementReplies: 0Last Post: 20th Mar 2014, 01:09 PM -
Styling <Object>
By Hillside in forum Web Development AreaReplies: 1Last Post: 10th Mar 2011, 04:56 AM -
Creating a Fast PHP Template System
By litewarez in forum Web Development AreaReplies: 7Last Post: 26th Feb 2011, 08:36 AM -
iFrame code or object help need
By lib3rty1 in forum Web Development AreaReplies: 2Last Post: 24th Nov 2010, 06:13 AM -
\Windows\System32\Config\System
By Rick in forum Technical Help Desk SupportReplies: 35Last Post: 24th Jan 2010, 10:36 PM
themaCreator - create posts from...
Version 3.51 released. Open older version (or...