function aliases

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


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?



Re: function aliases

Martijn wrote:

Quoted text here. Click to load it

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();
  case ('postgres'):
   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.

Re: function aliases

Hi Kenneth,

Thank you very much for your thoughts.

Quoted text here. Click to load it

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.

Quoted text here. Click to load 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

    switch ( DBTYPE )
    case ( 'mysql' ):    include_once('');    break;
    case ( 'mssql' ):    include_once('');    break;

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


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

But thanks again!


Site Timeline