1

基本上我创建了这个脚本来检查文件是否存在然后创建它。在我拥有它的非 OOP 版本之前,它运行良好。

现在我将它修改为 OOP 并且不知何故它不起作用,我在 Apache PHP Fatal error: Call to undefined function createFile() in C:\Program Files (x86)\Zend\Apache2\htdocs\Proj11\ 1.php在第66行

我突出显示了第 66 行与该行所在的位置//// THE ERROR LINE BELOW

它出什么问题了???谢谢

<?php 
//DB Config File



$phase = $_GET['phase'];

if(empty ($phase)){
    $phase = new phase1();
    $phase->start();
    } elseif ($phase = 1) {
        $phase = new phase2();
    $phase->stepFunction();
        };

class phase1 {

    function __construct () {
        $dbFile = 'dbconfig.php';
        $step = 0;
        $username = $_GET['username'];
        $password = $_GET['password'];
        $server = $_GET['server'];
        $dbName = $_GET['dbName'];

        $this->step = $step;
        $this->dbFile = $dbFile;
        $this->username = $username;
        $this->password = $password;
        $this->server = $server;
        $this->dbName = $dbName;

        $db = new PDO ('mysql:host=' .$server.';dbname='.$this->dbName,$this->username,$this->password);

        $this->db = $db;
        }


public function createFile () {
        //Creates File and populates it.
        $fOpen = fopen($this->dbFile, 'w');
            $fString .= "<?php\n";
            $fString .= "// Database Constants\n";
            $fString .= "\$DB_SERVER =" . "\"" . $this->server . "\";\n";
            $fString .= "\$DB_USER =" . "\"" . $this->username . "\";\n";
            $fString .= "\$DB_PASS =" . "\"" . $this->password . "\";\n";
            $fString .= "\$DB_NAME =". "\"" . $this->dbName . "\";\n";
            $fString .= "?>";

        fwrite($fOpen, $fString);
        fclose($fOpen);

    return true;
}   


public function start (){

try {

if ($this->db) { //if succesful at connecting to the DB

if (file_exists($this->dbFile)){
    if (is_readable($this->dbFile) && is_writable($this->dbFile)){ 

        //Creates File, populates it and redirects the user

  //////////////////////////
  //// THE ERROR LINE BELOW
  //////////////////////////

    if (createFile()) { 

        $phase = new phase2();
        $phase->stepFunction($this->step);
         exit ();
            }


        } else { 

        echo "The file {$dbFile} cannot be accessed. Please configure the file manualy or grant Write and Read permission.";  }

    } else {

        //Creates File, populates it and redirects the user

    if (createFile()) {


        $phase = new phase2();
        $phase->stepFunction($this->step);
         exit ();
            }

        }


}

} catch (PDOException $e) { //Catchs error if can't connect to the db.
    echo  'Connection failed: ' . $e->getMessage();
}

}
    } // en class Phase 1
4

1 回答 1

8

createFile()是类中定义的方法,必须在类中调用$this->createFile()

if ($this->createFile()) {...}

我还没有彻底检查你的代码,但你可能也忽略$this->了其他方法调用。

我还要指出,由于似乎没有任何情况会createFile()返回除 之外TRUE的任何内容,因此实际上不需要该if () {}块;案件将else永远无法到达。

于 2012-08-29T13:46:15.087 回答