note 85354 added to function.func-get-args
| From: | anderson at francotecnologia dot com | Date: | Wed, 27 Aug 2008 06:25:58 +0000 |
| Subject: | note 85354 added to function.func-get-args | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-143858@lists.php.net to get a copy of this message | ||
How to create a polymorphic/"overloaded" function
<?php
function select()
{
$t = '';
$args = func_get_args();
foreach ($args as &$a) {
$t .= gettype($a) . '|';
$a = mysql_real_escape_string($a);
}
if ($t != '') {
$t = substr($t, 0, - 1);
}
$sql = '';
switch ($t) {
case 'integer':
// search by ID
$sql = "id = {$args[0]}";
break;
case 'string':
// search by name
$sql = "name LIKE '%{$args[0]}%'";
break;
case 'string|integer':
// search by name AND status
$sql = "name LIKE '%{$args[0]}%' AND status = {$args[1]}";
break;
case 'string|integer|integer':
// search by name with limit
$sql = "name LIKE '%{$args[0]}%' LIMIT {$args[1]},{$args[2]}";
break;
default:
// :P
$sql = '1 = 2';
}
return mysql_query('SELECT * FROM table WHERE ' . $sql);
}
$res = select(29); // by ID
$res = select('Anderson'); // by name
$res = select('Anderson', 1); // by name and status
$res = select('Anderson', 0, 5); // by name with limit
?>
----
Server IP: 69.147.83.197
Probable Submitter: 201.95.126.5
----
Manual Page -- http://www.php.net/manual/en/function.func-get-args.php
Edit -- https://master.php.net/note/edit/85354
Del: integrated -- https://master.php.net/note/delete/85354/integrated
Del: useless -- https://master.php.net/note/delete/85354/useless
Del: bad code -- https://master.php.net/note/delete/85354/bad+code
Del: spam -- https://master.php.net/note/delete/85354/spam
Del: non-english -- https://master.php.net/note/delete/85354/non-english
Del: in docs -- https://master.php.net/note/delete/85354/in+docs
Del: other reasons-- https://master.php.net/note/delete/85354
Reject -- https://master.php.net/note/reject/85354
Search -- https://master.php.net/manage/user-notes.php