Quantcast
Channel: Squirrel Hacker » Value at risk
Viewing all articles
Browse latest Browse all 3

Documenting PHP Code

$
0
0

I know we all hate documenting code, but it can really help out future you or me when we need to go back and fix something. Poorly documented code is the bane of anyone who will be taking on your code after you leave (or invite more people into) the project. One thing that can really help is documenting the variables that you can get via the __get and __set magic methods:

<?php
 
/**
 * @property bool $development_environment
 * @property string $base_url
 * @property string $index_file
 * @property string $allowed_uri_characters
 * @property string $char_encoding
 * @property string $default_location
 * @property string $helper_prefix
 * @property array $auto_load
 * @property string $auto_load['helpers']
 * @property array $db
 * @property string $db['name']
 * @property string $db['user']
 * @property string $db['password']
 * @property string $db['host']
 * @property string $db['type']
 * @property array $users
 * @property string $users['table']
 * @property string $users['encryption']
 * @property array $users['fields']
 * @property string $users['session_name']
 */
class config{
	/**
	 * Get a var from the config values
	 * @param string $var
	 */
	function __get($var) {
		if(isset($this->data[$var])) {
			return $this->data[$var];
		}
		throw new Exception($var . ' does not exist in '. __CLASS__);
	}
 
	/**
	 * Set a new/current config value
	 * @param string $var
	 * @param multiple $value
	 * @return  multiple
	 */
	function __set($var, $value) {
		return $this->data[$var] = $value;
	}
 
}

Adding the @property values to the top of the class lets you see at a quick glance what default properties that the class gives you. It can even help you in your ide when you are trying to figure out what you can actually get out of a class:

Remember, try not to piss off future you, they know where you live.

Flattr this!


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images