基本上我创建了这个脚本来检查文件是否存在然后创建它。 在我有非OOP版本之前,它工作得很好。
现在我修改它成为OOP,不知何故它不起作用我在Apache中遇到错误 PHP致命错误:在C:\ Program Files(x86)\ Zend \中调用未定义函数createFile()第66行的Apache2 \ htdocs \ Proj11 \ 1.php
我突出显示第66行与行//// THE ERROR LINE BELOW
它有什么不对吗??? THX
<?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
答案 0 :(得分:8)
createFile()
是类中定义的方法,必须在类$this->createFile()
内调用:
if ($this->createFile()) {...}
我还没有仔细查看过您的代码,但您也可能在其他方法调用中省略了$this->
。
我还要指出,由于createFile()
似乎没有任何情况可以返回TRUE
以外的任何内容,因此不需要if () {}
块; <{1}}案例将永远无法访问。