所以我在尝试为我的程序构建类文件时遇到了一个奇怪的错误。在特别是面向对象的PHP之前,我从来没有遇到过这个问题。基本上解释它的最快方法是添加一个新文件使另一个文件不存在。我将在下面演示:
文件#1
//process.php
require_once '../../inc/config.php';
include 'ProcessApplication/ProcessApplication.php';
$processor = new ProcessApplication($CONN);
if($processor->getConn()){
list($general, $phone) = $processor->processGeneralInfo($_REQUEST['general_information'];
}
我不打算包含ProcessApplication.php文件中的所有代码,因为它不相关。
文件#2
class ProcessApplication{
protected $conn = null;
function __construct($CONN){
$this->conn = $CONN;
}
public function getConn(){
return $this->conn;
}
.
.
.
}//end ProcessApplication class
文件#3,确保继承正常工作的简单测试程序
class ProcessApplicationQuery extends ProcessApplication{
public function getReady(){
if($this->getConn()){ //edited as of most recent update
print "Ready";
}else{
print "not Ready";
}
}
}
到目前为止,文件#3未包括在内,该程序仍然有效。此时我已将它包含在FILE#1中,我不知道为什么
//process.php
require_once '../../inc/config.php';
include 'ProcessApplication/ProcessApplication.php';
include 'ProcessApllication/ProcessApplicationQuery.php'; //2nd class file added
$processor = new ProcessApplication($CONN);
if($processor->getConn()){
list($general, $phone) = $processor->processGeneralInfo($_REQUEST['general_information'];
$processor->getReady(); //added to check file #3 was working
}
现在,FILE#1已经更新。我得到Fatal Error: failed to open file stream '../../inc/config.php'
即使它工作正常,然后我撤消所有更新,并将FILE#1恢复到其原始状态,程序仍然失败。
基于我在PHP和JAVA中学到的关于面向对象编程的一切,这一小部分程序应该正常工作。除非它简单易懂,否则我无法看到我的错误在哪里。我真的很沮丧,我希望这对某人有意义。
=== UPDATES ===
文件#1现在看起来像这样,并且已经更新以匹配问题的正确解决方案
//process.php
include '../../inc/config.php';
include 'ProcessApplication/ProcessApplication.php';
include 'ProcessApplication/ProcessApplication.php';
$processor = new ProcessApplicationQuery($CONN);
if($processor->getConn()){
list($general, $phone) = $processor->processGeneralInfo($_REQUEST['general_information'];
$processor->getReady(); // this prints out "ready"
}
答案 0 :(得分:1)
因为ProcessApplicationQuery继承了ProcessApplication中的所有内容,所以您不需要两者:
$processor = new ProcessApplication($CONN);
$query = new ProcessApplicationQuery();
这会创建两个不同的对象,$ processor是唯一具有$ CONN的对象。
相反,将$ CONN直接传递给最终类,例如:
$query = new ProcessApplicationQuery($CONN);