Undefined variable problem

Robert Francis
Robert Francis used Ask the Experts™
on
Just starting to learn oo php. Probably a simple problem.

Here is some of the code from header.php

$serverName = "server\\exactsqlexpress"; 
$connectionInfoIntranet = array( "Database"=>"intranet", "UID"=>"???", "PWD"=>"???");
$connectionInfoPpp = array( "Database"=>"ppp", "UID"=>???", "PWD"=>"???");

Open in new window


Here is some of the code from dbIntranet.php
include("header.php");

class dbIntranet {

	private $connIntranet;
	
	function __construct(){
			$this->open_connection();
	}
	
	public function open_connection() {
			$this->connIntranet = sqlsrv_connect($serverName, $connectionInfoIntranet); <-----LINE 13
	}

Open in new window


I keep getting an error:

Notice: Undefined variable: serverName in C:\inetpub\wwwroot\portal2\inc\dbIntranet.php on line 13

What's up?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
header.php is defining $serverName in the global scope. Code inside of a function or a class method can't see the global scope automatically - they only see the variables that are passed in or otherwise made explicitly available to them.

So you -could- do this to your class definition:
class dbIntranet {

      private $connIntranet;
      private $serverName; // New class property called serverName
      
      function __construct($serverName){ // Change the constructor to accept a new parameter
                 $this->serverName = $serverName; // Copy the value of the parameter to the class property
                  $this->open_connection();
      }
      
      public function open_connection() {
                  $this->connIntranet = sqlsrv_connect($this->serverName, $connectionInfoIntranet); <-----LINE 13
      }

...and then when you created a new instance of dbIntranet, you pass in your global variable:

$x = new dbIntranet($serverName);
Commented:
The same goes for $connectionInfoIntranet, by the way - it's defined as a global variable in your header, but it isn't passed in to your class anywhere, so it would ALSO throw that same undefined variable error unless you corrected it in the same way.

Also, some people might suggest using the "global" keyword to import the variable from the global scope into the local method/function scope like this:

public function open_connection() {
{
 global $serverName;
  global $connectionInfoIntranet;
 $this->connIntranet = sqlsrv_connect($serverName, $connectionInfoIntranet);
}

...but that is a BAD practice/design. It -will- work from a technical perspective, but it breaks the object-oriented nature of your class by adding dependencies to code and variable names that exist outside your class.
Robert FrancisDirector of Continuous Improvement

Author

Commented:
Would I not have to do the same thing for $connectionInfoIntranet?
Robert FrancisDirector of Continuous Improvement

Author

Commented:
Oops, we commented at the same time
Robert FrancisDirector of Continuous Improvement

Author

Commented:
Thanks for the lesson.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial