Archive for the ‘Fun’ Category

Function Parameter Hell

leave a comment

Everyone’s used those functions with an obscene number of parameters. And everyone’s created those functions too (big time guilty here).
Like this is a function I wrote for vaCentral to process any images you upload for your airline (logo, gallery, etc)

function process_file_upload($file, $slug, $title, $ext='PNG', $full_size=300, $small_size=100)
{
	...
}

Now that’s just a mess. Taking a hint from Javascript and jQuery, we can instead use arrays to pass in parameters, making it sane, and more importantly, readable. So it will look something like this:

$options = array (
	'file'=>$this->params['form'][$image],
	'slug'=>$folder,
	'title'=>$image,
	'ext'=>$ext,
	'full_size'=>$large_size,
	'small_size'=>$this->small_size
	);

$this->process_image_upload($options);

Alright, that sounds better. And looks much better, and I can actually understand what’s going on. But how will we work with this? As you’ve probably noticed, default parameters are gone. Here’s the code:

// Setup our options for what we want to pass
$options = array(
	'parameter1'=>'apples',
	'parameter3'=>'grapes',
	);

some_function($options);

function some_function($options)
{
	// Now these are the defaults
	$defaults = array(
		'parameter1'=>'oranges',
		'parameter2'=>'watermelons',
		'parameter3'=>'grapefruit',
		'parameter4'=>''
	);

	/* Now we merge the two arrays, except the $options
		array takes precedence */
	$options = array_merge($defaults, $options);

	/* extract() creates a variable with every key from the array
		so we can use that variable directly, this is optional */
	extract($options);

	// So now we can use the variables directly
	echo $parameter1; // This will echo "apples"
	echo $parameter2; // This will echo "watermelons"

	/* Suppose parameter4 has to be set, so we will trigger an error
		You can use throw as well if it's in a try/catch block */
	if($parameter4 == '')
	{
		trigger_error('Parameter 4 must be set to "this value" or "that value"', E_USER_WARNING);
	}

	/* Or, if you assume that the $defaults set with '' are mandatory to fill out,
		you can use this code */
	foreach($defaults as $name=>$value)
	{
		if($values == '' && $options[$name] == '' || !isset($options[$name]))
		{
			trigger_error("{$name} is blank, must have a value", E_USER_WARNING);
		}
	}
}

Now obviously this is a little more involving, but it’s not too bad, and in my opinion, it’s well worth the extra effort needed. My tipping point is functions with more than 3 parameters will get array’d options. This also requires documentation, but I don’t think that’s such a bad thing (your sanity will thank you later). You can also peek into the function’s source code to see the parameters. But this also gives you a bit more control over error messages as well, with the trigger_error, so there’s no difference from how PHP natively reports it.

Also, another advantage is adding and removing parameters. You don’t need to reorder or completely destroy things, when in the future you decide to add or remove a parameter, which makes it real handy for API type functions. Debugging and logging can also be easier, since you can dump the entire parameters array in one swoop, without having to output every single variable, one at a time. The only drawback is the lack of restrictions on a blank value, but since there is a way around it, and you’re forced to check it, IMO that’s an advantage in the end, especially since my motto is to never trust any data coming into the system, especially in an API.

Written by Nabeel

October 9th, 2009 at 11:10 pm

Posted in Fun, php

ezSQL Database Library (Improved!)

9 comments

Note: I’ve made a dedicated page for ezSQL-related items, you can view it here

I’ve been using ezSQL for a long time, as my “database driver”. It’s an awesome little class, that you can easily use to get database results, and return in different formats. I’ve made a bunch of changes and updates to it, so I thought I’d put them out there and share them.

Along the way, I’ve made some updates and changes to it, to suite my requirements, including format PHP5 support. I’ve updated the error handling/logging, so now you can call:

$sql->error(); // Get the error string
$sql->errno(); // Get the error number

Respectively after queries to return the “real” status of a query. the debug() function has been added to, so at call time, you can pass to bool whether to display the result on the screen, or pass it back as a string

I’ve also added several utility functions:

$sql->quick_select();
$sql->quick_update();
$sql->quick_insert();

// quick_select() example:
$columns = array('column1', 'column2');
$sql->quick_select('table_name', $columns, 'LIMIT 10');

To make it easier to do simple SELECT’s and UPDATEs, and INSERTs.

Another thing it was missing with MySQLi support, so that has been added (though I have not had the time to add support for statements (though that can be accessed rather easily). I had added MSSQL support, but the file has gone AWOL (Icreated and used it for a specific project a while ago). I will update when I get a hold of it.

And another thing I added was a static interface class, which I use all the time on PHP5 projects:

DB::init('mysql');
DB::connect('username', 'password', 'database_name', 'localhost');
// Now anywhere in your script:
$row = DB::get_row('...');
// Or
$results = DB::get_results('...', ARRAY_A);

Makes it much easier to access the database function, without having to do $db = DB::getInstance(), in every single function to get a singleton object of the database. Since that’s for PHP5 only, the whole library has been updated for PHP 5 support, with public/private functions and constructors/destructors as well.

I also condensed it to just one include() now, just keep all the files in the same place (include ‘…/DB.class.php’). From that you can either use the static class, or declare a new ezSQL object.

I also added in phpDoc blocks on all the functions, since IntelliSense is awesome and really helpful.

I hope these improvements make it easier for everyone. Of course, the original credit goes to Justin Vincent. There are some docs and examples here as well.

You can download it from here

Written by Nabeel

February 3rd, 2009 at 11:52 am

Posted in Fun, General, Projects, php

Airplane Movie Quotes

one comment

I just did a quick thing for the phpVMS admin panel, displaying random quotes from the movie Airplane! in the footer. Just call the function randquote(), and it’ll return the string, so you can do whatever you want with it.

And don’t call me Shirly…

Click to download it (zip file)

Written by Nabeel

January 2nd, 2009 at 12:26 pm

Posted in Fun, General, phpVMS

Tagged with , ,