为什么file_get_contents()会返回错误"文件名不能为空"?

Joh*_*aft 6 php

我几乎是PHP的新手.我的背景是C/C++和C#.我试图反对orient-ify一些简单的PHP代码,但我做错了.

班级代码:

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];
  }
}
Run Code Online (Sandbox Code Playgroud)

来电代码:

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

file_get_contents($Filename)在行上说明错误:文件名不能为空.如果我硬编码文件名代替$ Filename,那么我只需获取字段的所有空字符串.

我错过了什么简单的概念?

Gre*_*reg 12

你的情况有误:

file_get_contents($Filename);
Run Code Online (Sandbox Code Playgroud)

应该

file_get_contents($FileName);
Run Code Online (Sandbox Code Playgroud)

您应该在php.ini文件中或使用error_reporting()打开Notices