You are here: Suggestion for porting function to PHP5?? « PHP Programming Language « IT news, forums, messages
Suggestion for porting function to PHP5??

Posted by Tim Meader on 08/23/06 16:06

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_stInputString)
{
if (isset($this->hDB)) {
return mysql_real_escape_string($a_stInputString, $this->hDB);
}

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

$l_hDBForFunc =
mysql_connect($l_stServer,$l_stUsername,$l_stPassword,FALSE,2);
return mysql_real_escape_string($a_stInputString, $l_hDBForFunc);
}

"escape_tester" 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::doEscapeString. 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_stInputString)
{
$l_stServer = 'localhost';
$l_stUsername = 'escape_tester';
$l_stPassword = 'a1b2c3d4';

$l_hDBForFunc =
mysqli_connect($l_stServer,$l_stUsername,$l_stPassword);
return mysqli_real_escape_string($l_hDBForFunc,$a_stInputString);
}

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_roDBConn = new DBConn" was created prior to ever
calling "doEscapeString". In those cases, I would later use
"$l_roDBConn->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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация