tags:

views:

104

answers:

5

I'm pretty much a complete newbie to PHP. My background is C/C++ and C#. I'm trying to object orient-ify some simple PHP code, but I'm doing something wrong.

Class code:

class ConnectionString
{
  public $String = "";
  public $HostName = "";
  public $UserName = "";
  public $Password = "";
  public $Database = "";

  function LoadFromFile($FileName)
  {
    $this->String = file_get_contents($Filename);
    $Values = explode("|", $this->String);
    $this->HostName = $Values[0];
    $this->UserName = $Values[1];
    $this->Password = $Values[2];
    $this->Database = $Values[3];
  }
}

Calling code:

$ConnectionString = new ConnectionString();
$FileName = "db.conf";
$ConnectionString->LoadFromFile($FileName);
print('<p>Connection Info: ' . $Connection->String . '</p>');

I'm getting ann error on the file_get_contents($Filename) line stating: Filename cannot be empty. If I hard-code the filename in place of $Filename, then I simply get all empty strings for the fields.

What simple concept am I missing?

+8  A: 

You've got the case wrong:

file_get_contents($Filename);

should be

file_get_contents($FileName);

You should turn on Notices, either in your php.ini file or using error_reporting()

Greg
Gotta love dynamic languages here.
Michael Myers
Wow. I knew it was something stupid. Thanks (everyone) for the help. I actually didn't realize that PHP was case sensitive. I'm lucky I've gotten this far. @Greg, thanks for the tip on NOtices and error_reporting(). I wasn't aware of them.
John Kraft
+1  A: 

Variable case-sensitivity:

function LoadFromFile($FileName)
{
   $this->String = file_get_contents($Filename); // This should be $FileName!
PatrikAkerstrand
+1  A: 
$this->String = file_get_contents($FileName);

you have $Filename

Galen
+3  A: 

Variables in PHP are case-sensitive. You've defined $FileName as a parameter to the LoadFromFile() method, but you used $Filename on the first line of that method. For more information about PHP variables:

http://www.php.net/manual/en/language.variables.basics.php

There are a few things you can do to avoid this problem in the future:

  • Use an IDE, such as Eclipse PDT, that support auto-completion of variables.
  • Configure error_reporting to display all types of errors (E_ALL).
Jordan Ryan Moore
+1  A: 
 $this->String = file_get_contents($Filename);

On this line, you write $File**n**ame when it should be $File**N**ame

transmogrify