webguync Posted February 19, 2010 Share Posted February 19, 2010 Hi all, I inherited code from a previous developer which was specific to PHP 4.0 and we have upgraded, finally to 5.0+ and I need to know what needs to be changed the following code (if anything) to get it up to 5.0 standards. <?php //uses PHP4 class Database { var $host; var $user; var $pwd; var $dbName; var $flash; var $dbLink; var $result; var $resultObj; function Database($host, $user, $pwd, $dbName, $flash=1){ $this->host = $host; $this->user = $user; $this->pwd = $pwd; $this->dbName = $dbName; $this->flash = $flash; $this->connect(); } //Connect to MySQL server and select database function connect() { $this->dbLink = @mysql_pconnect($this->host, $this->user, $this->pwd); if(!$this->dbLink) { $error = 'Couldn\'t connect to the MySQL server.'; echo $this->flash ? 'error='.urlencode($error) : $error; exit(); } if(!mysql_select_db($this->dbName, $this->dbLink)){ $error = 'Couldn\'t open Database: '.$this->dbName; echo $this->flash ? 'error='.urlencode($error) : $error; exit(); } return $this->dbLink; } //Execute a SQL query function query($query) { $this->result = mysql_query($query, $this->dbLink); if (!$this->result) { $error = 'MySQL Error: '.mysql_error(); echo $this->flash ? 'error='.urlencode($error) : $error; exit(); } //store result in new object to emulate mysqli OO interface $this->resultObj = new MyResult($this->result); return $this->resultObj; } function close(){ //close MySQL connection mysql_close($this->dbLink); } } class MyResult { var $theResult; var $num_rows; function MyResult(&$r) { $this->theResult = $r; } function get_rows() { //get total number of records found $this->num_rows = mysql_num_rows($this->theResult); return $this->num_rows; } //fetch associative array of result (one row at a time) function fetch_assoc() { $newRow = mysql_fetch_assoc($this->theResult); return $newRow; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/192670-need-help-adjusting-40-specific-code-to-50/ Share on other sites More sharing options...
brosskgm Posted February 19, 2010 Share Posted February 19, 2010 I moved a lot of code from 4.0 to 5.0 and it was very little. Best way is to run it on 5.0 and watch for the error's on the screen and the httpd logs. Then that will tell you what needs to be fixed or the warnings. Quote Link to comment https://forums.phpfreaks.com/topic/192670-need-help-adjusting-40-specific-code-to-50/#findComment-1015005 Share on other sites More sharing options...
mikesta707 Posted February 19, 2010 Share Posted February 19, 2010 that looks ok. One thing is you don't need to use the keyword "var" anymore to define a class variable. but you may want to alter the scope of some of those variables if you wish to take advantage of encapsulation Quote Link to comment https://forums.phpfreaks.com/topic/192670-need-help-adjusting-40-specific-code-to-50/#findComment-1015016 Share on other sites More sharing options...
webguync Posted February 19, 2010 Author Share Posted February 19, 2010 so it can just be: class Database { $host; $user; $pwd; $dbName; $flash; $dbLink; $result; $resultObj; without var? Quote Link to comment https://forums.phpfreaks.com/topic/192670-need-help-adjusting-40-specific-code-to-50/#findComment-1015026 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.