468,257 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

function aliases

Hi,

I have to write a dynamic page which has to run on several platforms, using
different databases. Because of that, I want to write a small db-front-end
which is an almost 1-on-1 mapping of the functions provided by the PHP API
(and the proper front-end is loaded using a conditional include).

Using the "C-way", I thought of using define(), but in PHP that can only be
used for defining constants. I also though of using function variables, but
then the feel of the code changes.

The simplest method I have found sofar is the following (for mysql_connect):

function sql_connect()
{
$p = func_get_args();
return ( call_user_func_array('mysql_connect', $p) );
}

Anybody got any other ideas?

Thanks,
--
Martijn
http://www.sereneconcepts.nl
Jul 17 '05 #1
2 1657
Martijn wrote:

The simplest method I have found sofar is the following (for
mysql_connect):

function sql_connect()
{
$p = func_get_args();
return ( call_user_func_array('mysql_connect', $p) );
}

Anybody got any other ideas?

Thanks,


You might define a handful of classes, one for each db platform, all of
which have the same functions (*query, *fetch_array etc). That is a nice
"oopy" way to do things.

If you have no great love of object-orientation, you just have a file with
lots of switches in it. Which, IHMO, is much easier to maintain:

function sql_connect()
{
$p = get_env_variable("platform");
switch ($platform) {
case ('mysql'):
return whatever();
break;
case ('postgres'):
...
default:
return false;
}
}
Its the same typing in either case, but in the latter you have everything in
one place where you can see it.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #2
Hi Kenneth,

Thank you very much for your thoughts.
The simplest method I have found sofar is the following (for
mysql_connect):

function sql_connect()
{
$p = func_get_args();
return ( call_user_func_array('mysql_connect', $p) );
}

Anybody got any other ideas?
You might define a handful of classes, one for each db platform, all
of which have the same functions (*query, *fetch_array etc). That is
a nice "oopy" way to do things.


That would be a nice thing to do. No problem with OO, but it is just that I
am looking for a quick (and non-OO) way to do this. For my bigger projects
I usually have some stub-objects to communicate with the database.
If you have no great love of object-orientation, you just have a file
with lots of switches in it. Which, IHMO, is much easier to maintain:

[code snipped]

Its the same typing in either case, but in the latter you have
everything in one place where you can see it.


Its not the problem with the conditional includes that I am having, in fact,
I could still apply the switch approach using the above method (in fact, the
switches are there, but just one level higher - this also has the advantage
that it needs to be called only once for each script, and not on each
invocation of a database function). If anyone is interested, it is like
this:

switch ( DBTYPE )
{
case ( 'mysql' ): include_once('db_mysql.inc.php'); break;
case ( 'mssql' ): include_once('db_mssql.inc.php'); break;
}

(in fact, I have thought about making one statement:

include_once('db_'.DBTYPE.'.inc.php');

but this has the advantage that you see which dbs are supported)

What I am having problems with is easily/efficiently setting up a function
alias.

But thanks again!

--
Martijn
http://www.sereneconcepts.nl
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mike Ash | last post: by
5 posts views Thread by Kevin | last post: by
reply views Thread by Krzysiek | last post: by
22 posts views Thread by mp | last post: by
15 posts views Thread by jacob navia | last post: by
22 posts views Thread by Daniel Rucareanu | last post: by
11 posts views Thread by Chris Thomasson | last post: by
6 posts views Thread by dom.k.black | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.