Jump to content

Site Config, class or global?


Stooney

Recommended Posts

There's not such thing as "bad". Though lets indulge and call it "bad design", which one could interpret as "unwise" or "will come back and kick you in the ass sooner or later".

 

As for an example.. I'm pretty sure I've posted a snippet on this topic here and there, as well as others. So search first, ask later.

Link to comment
Share on other sites

I don't recommend hardcoding configuration options. Use an INI or XML file. But, if you insist on using constants, at least use CLASS constants.

 

Wouldn't parsing an INI or XML file be rather expensive?  Wouldn't it be better to at least cache the output into something more immediately useful, say using var_export?

Link to comment
Share on other sites

Wouldn't parsing an INI or XML file be rather expensive?  Wouldn't it be better to at least cache the output into something more immediately useful, say using var_export?

 

Yes and yes (tough simply serializing a resulting object would be preferable).

 

Usually posting reasons why it's bad is more effective than just telling them not to do it.

 

Well, it's simple enough.

 

Imagine looking at someone else's code (or your own code that you wrote well over a year ago), and finding this:

 

public function doSomething($foo){
   global $foo, $bar, $meh, $blah;
   //Do something with all this global data
}

 

Now let's work under the insane presumption that the method does not work properly (I'm sure that never happens to you, but stupid people like myself do get faced with unwanted behaviour in their applications sometimes).

 

Where does the data come from? Where is it set, where is it manipulated... What the hell is even in there? Can you tell?

 

The same goes for ALL global data.

 

Constants:

 

function doSomethingElse(){
     $contents = file_get_contents(MYDIR . DIRECTORY_SEPARATOR . 'file.txt');
}

 

Where is MYDIR set? What is the meaning of MYDIR? What if MYDIR is not defined? What happens if MYDIR is empty? Hell if know.

 

Singletons, global Registry:

 

function doSomethingComplicated(){
    $object = SomeSingleton::getInstance();
    //Do something with object
}

 

Well, at least I know what is in there. It's the SomeSingleton. Joy. It is however impossible to trace all the objects who have used this object before me (like the slut that it is), and what they did to it. Also, it is not apparent from the API that this class uses SomeSingleton.

 

The more you code OO, the more you will find that what seems convenient at first, turns out to be a big pain in the ass.

 

 

 

Link to comment
Share on other sites

Actually parsing an XML/INI file can be done once and your application XML options cached in a serialized file.

Here's a snippet from some work i have been doing recently:

/**
 * Initialises ApplicationRegistry
 *
 */
public function init(){
	// The the Registry_Application isn't empty, then return.
	if(!Registry_Application::instance()->isEmpty()){
		echo "App registry is NOT empty!<br/>";
		//return;
	}
	// Otherwise, this is the first run and the app registry is empty, so load it up.
	$this->getOptions();
}

 

This is the application helper class. init() is run each time the helper is loaded.

The application registry maintains a file that contains serialised objects.

When the instance() of the Registry_Application is loaded it reads the file, and if it's empty isEmpty() returns true.

If it IS empty then the internal getOptions() is run which loads the xml file and parses it loading up all your config options.

It sets all the relevant options inside the Registry_Application which calls save() on it's destructor method which saves all the info into the txt file in serialised format.

Link to comment
Share on other sites

  • 2 weeks later...

Constant is not a singleton and singletons are not a pattern which should be used for tracking constants. Define is not bad. It just should be used wisely. It is like makros in C. Many will say it is bad and never should be used but other will give plenty of examples where you can use them.

 

Define should be used project wide config files which will be never changed. This values shouldn't be set by user. They shouldn't be computed.

define('PI',3.14)

 

If you need some constant inside a code you should use const keyword.

http://pl2.php.net/manual/en/language.oop5.constants.php

 

Configuration shouldn't be some global. If configuration of site is simple (not too many options) it can be one singleton with __get function. If it is complicated I would create set of singletons one for each group of options.

Ex:

  DatabaseConfiguration

  PathConfiguration

  ...

 

Each class will be keeping track of its data. Is it file, database or whatever. In a big project some options probably will be taken from file, some from database and some computed.

 

Site Config, class or global? Singleton and data should be stored outside php.

Link to comment
Share on other sites

I would store site information, like DB connection, in an XML file. Then just write a little class that use SimpleXML, and create a static function which returns the config info as an array or stdClass. Then just call that static method the __construct of all necessary classes.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.