Solved

Passed by reference notices in legacy php code

Posted on 2012-03-21
2
296 Views
Last Modified: 2012-04-02
I have inherited some legacy code that is throwing some php notices which I would like to fix.  Here is the current notice:
PHP Notice:  Only variable references should be returned by reference in \wwwroot\classes\model\DatabasePeer.php on line 40

Open in new window


and here is the code in DatabasePeer.php around line 40 (line 40 is line 2 here):
public static function &connect($host,$user,$pass) {        			
        		return @mssql_connect($host, $user, $pass);
        		return 1;
        }

Open in new window


This function is called with the following code:
$conn = DatabasePeer::connect($CONFIG_DATABASE_HOST, $CONFIG_DATABASE_USERNAME, $CONFIG_DATABASE_PASSWORD);
try{
	$sqldb = DatabasePeer::select_db($CONFIG_DATABASE_NAME, $conn);
}catch(Exception $e){
	die("Couldn't open database $CONFIG_DATABASE_NAME".$e);
	exit();
}

Open in new window


Any thoughts how I can change this code to prevent the notice?
0
Comment
Question by:wardjame
2 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 37750052
If the code is working correctly, you can just suppress the Notice with this error reporting:

error_reporting(E_ALL ^ E_NOTICE);

Notice suppression is the standard "default" case for most PHP installations.
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 37751121
That doesn't look right to me with two return statements.  It should be returning the 'resource' which is normally assigned to a variable.  Maybe this?

public static function &connect($host,$user,$pass) {        			
        		$resource = @mssql_connect($host, $user, $pass);
        		return $resource;
        }

Open in new window


http://us3.php.net/manual/en/function.mssql-connect.php

Also note that if you upgrade to PHP 5.3 or greater, the 'php_mssql' functions are obselete, will not work, and are replaced by the Microsoft PHP SQL driver as noted here: http://us3.php.net/manual/en/intro.mssql.php   The current version is here http://www.microsoft.com/download/en/details.aspx?id=20098 and will not work on anything older than Windows Vista.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question