Undefined variable problem

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?
Robert FrancisDirector of Continuous ImprovementAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
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);
0
gr8gonzoConsultantCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Robert FrancisDirector of Continuous ImprovementAuthor Commented:
Would I not have to do the same thing for $connectionInfoIntranet?
0
Robert FrancisDirector of Continuous ImprovementAuthor Commented:
Oops, we commented at the same time
0
Robert FrancisDirector of Continuous ImprovementAuthor Commented:
Thanks for the lesson.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.