function aliases

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martijn

    #1

    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_co nnect', $p) );
    }

    Anybody got any other ideas?

    Thanks,


    --
    Martijn



  • Kenneth Downs

    #2
    Re: function aliases

    Martijn wrote:
    [color=blue]
    >
    > 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_co nnect', $p) );
    > }
    >
    > Anybody got any other ideas?
    >
    > Thanks,
    >
    >[/color]

    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_variabl e("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 )

    Comment

    • Martijn

      #3
      Re: function aliases

      Hi Kenneth,

      Thank you very much for your thoughts.
      [color=blue][color=green]
      >> 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_co nnect', $p) );
      >> }
      >>
      >> Anybody got any other ideas?[/color]
      >
      > 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.[/color]

      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.
      [color=blue]
      > 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.[/color]

      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('d b_mysql.inc.php '); break;
      case ( 'mssql' ): include_once('d b_mssql.inc.php '); break;
      }

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

      include_once('d b_'.DBTYPE.'.in c.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.sereneconce pts.nl


      Comment

      Working...