Suggestion for porting function to PHP5??

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

    Suggestion for porting function to PHP5??

    Hello, I was hoping someone could provide some insight as to the best
    approach to take in porting over a function I use in quite a few spots
    from PHP4 to PHP5. Basically, I have a db wrapper class that I use
    called DBConn (this is the PHP5 ported definition for the members, PHP4
    was the same except "private" was "var"):

    class DBConn
    {
    private $hDB; //db link
    private $bConnected; //db link status
    private $bInTX; //current transaction status, used to avoid
    deadlocks
    private $hResult; //result link
    private $iNumResultRows ;
    }

    with those being the members. Within this class, I also have the
    following method (this is the PHP4 definition for the method):

    function doEscapeString( $a_stInputStrin g)
    {
    if (isset($this->hDB)) {
    return mysql_real_esca pe_string($a_st InputString, $this->hDB);
    }

    $l_stServer = 'localhost';
    $l_stUsername = 'escape_tester' ;
    $l_stPassword = 'a1b2c3d4';

    $l_hDBForFunc =
    mysql_connect($ l_stServer,$l_s tUsername,$l_st Password,FALSE, 2);
    return mysql_real_esca pe_string($a_st InputString, $l_hDBForFunc);
    }

    "escape_tes ter" is a completely unprivileged account, used only for the
    escape_string function connection when necessary. As you can see, in
    PHP4, since it doesn't do such strict checking, you are allowed to
    check for the existence of the "$this" variable prior to creating the
    new connection, even when you call the method statically via
    DBConn::doEscap eString. Obviously, this has changed in PHP5 thanks to
    the new object implementation, and "$this" never exists for a static
    public function (as you have to change doEscapeString to in order to
    not cause an error when calling it statically in your code). Here is
    the PHP5 version:

    public static function doEscapeString( $a_stInputStrin g)
    {
    $l_stServer = 'localhost';
    $l_stUsername = 'escape_tester' ;
    $l_stPassword = 'a1b2c3d4';

    $l_hDBForFunc =
    mysqli_connect( $l_stServer,$l_ stUsername,$l_s tPassword);
    return mysqli_real_esc ape_string($l_h DBForFunc,$a_st InputString);
    }

    What I'm trying to d is figure out a way that I can still have a test
    for the existence of a current db link prior to ALWAYS creating a new
    connection for each call to doEscapeString. There are quite a few spots
    in my code where a "$l_roDBCon n = new DBConn" was created prior to ever
    calling "doEscapeString ". In those cases, I would later use
    "$l_roDBCon n->doEscapeString (xxx)" instead of calling it statically...
    thus reusing the existing connection. This no longer is possible with
    PHP5 though unfortunately. Any suggestions as to what could be done to
    regain the old PHP4 functionality? Thanks in advance for any and all
    replies.

    Tim

Working...