0
function query($query, $cacheType, $cacheDuration, $responseType)

$query = Query to be executed.
$cacheType = Memcached etc.
$cacheDuration = Duration of cache.
$responseType = MySQL response type. (e.g array, json etc)
  1. I don't want to use cache, but expect to be JSON. How can I pass those 2 parameters?

  2. If I set cacheType to "Memcached", how can I ensure a $cacheDuration is set to a valid integer?

  3. If I add a fifth parameter to query function (e.g $fifth), how can I add it without breaking the calls used in entire website? They still give 4 parameters, but query updated to be 5 parameters.

I'm looking for "good practise" responses mostly, otherwise I know passing NULL values to params.

Lisa Miskovsky
  • 896
  • 2
  • 9
  • 18

3 Answers3

2
$paramsArray = array();
$paramsArray['query'] = 'Query to be executed.';
$paramsArray['cacheType'] = 'Memcached etc.';
$paramsArray['cacheDuration'] = 'Duration of cache.';
$paramsArray['responseType'] = 'MySQL response type. (e.g array, json etc)';

echo query($paramsArray);

function query($paramsArray = array()) {

  $query = $paramsArray['query'];
  $cacheType = $paramsArray['cacheType'];
  $cacheDuration = $paramsArray['cacheDuration'];
  $responseType = $paramsArray['responseType'];

  // Your query operations with $query, $cacheType, $cacheDuration, $responseType
}
Dino Babu
  • 5,814
  • 3
  • 24
  • 33
0

use fifth parameter like this by setting default value, it will not break existing function call.

function query($query, $cacheType, $cacheDuration, $responseType, $fifth = NULL)

you can call this function with 4 parameter also

echo query($query, $cacheType, $cacheDuration, $responseType);
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

I don't want to use cache, but expect to be JSON. How can I pass those 2 parameters?

Didn't understand.

If I set cacheType to "Memcached", how can I ensure a $cacheDuration is set to a valid integer?

I probably didn't understand that one either but you can just validate with the is_int() function.

If I add a fifth parameter to query function (e.g $fifth), how can I add it without breaking the calls used in entire website? They still give 4 parameters, but query updated to be 5 parameters. I'm looking for "good practice" responses mostly, otherwise I know passing NULL values to params.

I was just going to tell you that you could overload the function, but I just found out that it's not supported in php (PHP function overloading), so you'd probably be good adding a fifth parameter with a default value of null and just doing whatever you want when $fifth != null

Community
  • 1
  • 1
tonyjmnz
  • 536
  • 4
  • 13
  • 1. query($query, 'JSON'); 2. query($query, 'MEMCACHED', (Need an integer here or exception.)) 3. That's not something I want to do. – Lisa Miskovsky Feb 28 '13 at 04:17