Solved

Passed by reference notices in legacy php code

Posted on 2012-03-21
2
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 110

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

732 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