我在端口81上运行Apache。我的项目文件夹是MyPhpProject。在里面我有2个文件夹:Domain和Testing。
在 Domain 文件夹中,我有3个PHP文件:
Employee类有一个Location类的引用。
这是BaseDomain.php:
<?php
abstract class BaseDomain {
}
?>
这是Location.php:
<?php
$returnRequire = require 'BaseDomain.php';
class Location extends BaseDomain {
private $locationIdInt;
private $codeNameString;
private $descString;
public function setLocationId($locationId) {
$this->locationIdInt = $locationId;
}
public function getLocationId() {
return $this->locationIdInt;
}
public function setCodeName($codeName) {
$this->codeNameString = $codeName;
}
public function getCodeName() {
return $this->codeNameString;
}
public function setDesc($desc) {
$this->descString = $desc;
}
public function getDesc() {
return $this->descString;
}
}
?>
这是Employee.php:
<?php
$returnRequire = require 'BaseDomain.php';
class Employee extends BaseDomain {
private $employeeIdString;
private $locationObject;
public function setEmployeeId($employeeId) {
$this->employeeIdString = $employeeId;
}
public function getEmployeeId() {
return $this->employeeIdString;
}
public function setLocation($location) {
$this->locationObject = $location;
}
public function getLocation() {
return $this->locationObject;
}
}
?>
现在在 Testing 文件夹中我创建了一个Test_Employee.php,这是它的代码:
<?php
set_include_path('../Domain');
$getIncludePath = get_include_path();
echo "getIncludePath = " . $getIncludePath;
echo "<br>";
$returnRequire1 = require 'Location.php';
echo "returnRequire for Location.php = " . $returnRequire1;
echo "<br>";
$returnRequire2 = require 'Employee.php';
echo "returnRequire for Employee.php = " . $returnRequire2;
echo "<br>";
?>
当我尝试运行它http://localhost:81/MyPhpProject/Testing/Test_Employee.php
时,我遇到了无法重新声明BaseDomain类的致命错误。这是我在浏览器中看到的:
getIncludePath = ../Domain
returnRequire for Location.php = 1
致命错误:无法在C:\ Program Files中重新声明类BaseDomain (x86)\ Apache软件 基础\ Apache2.2 \ htdocs \ MyPhpProject \ Domain \ BaseDomain.php在线 2
我没有多次创建BaseDomain类。所以这个错误是奇怪的。有人可以解释为什么我收到错误消息?以及如何解决它。
感谢您的时间。
答案 0 :(得分:0)
行$returnRequire1 = require 'Location.php';
加载Location.php,后者又在行$returnRequire = require 'BaseDomain.php';
中加载BaseDomain.php。然后,行$returnRequire2 = require 'Employee.php';
加载Employee.php,它再次加载BaseDomain.php(行$returnRequire = require 'BaseDomain.php';
)。 BaseDomain.php的第二次加载导致php尝试重新定义BaseDomain类,这是不允许的。
解决此问题的最简单方法是将require
来电更改为require_once
。这将确保每次运行时每个文件只加载一次,这将防止您遇到的错误。
答案 1 :(得分:0)
BaseDomain.php:
<?php
abstract class BaseDomain {
}
?>
Location.php
<?php
class Location extends BaseDomain {
private $locationIdInt;
private $codeNameString;
private $descString;
public function setLocationId($locationId) {
$this->locationIdInt = $locationId;
}
public function getLocationId() {
return $this->locationIdInt;
}
public function setCodeName($codeName) {
$this->codeNameString = $codeName;
}
public function getCodeName() {
return $this->codeNameString;
}
public function setDesc($desc) {
$this->descString = $desc;
}
public function getDesc() {
return $this->descString;
}
}
?>
Employee.php:
<?php
class Employee extends BaseDomain {
private $employeeIdString;
private $locationObject;
public function setEmployeeId($employeeId) {
$this->employeeIdString = $employeeId;
}
public function getEmployeeId() {
return $this->employeeIdString;
}
public function setLocation($location) {
$this->locationObject = $location;
}
public function getLocation() {
return $this->locationObject;
}
}
?>
Test_Employee.php
<?php
set_include_path(__DIR__.'/MyPhpProject/Domain');
require 'BaseDomain.php';
$getIncludePath = get_include_path();
echo "getIncludePath = " . $getIncludePath;
echo "<br>";
$returnRequire1 = require 'Location.php';
echo "returnRequire for Location.php = " . $returnRequire1;
echo "<br>";
$returnRequire2 = require 'Employee.php';
echo "returnRequire for Employee.php = " . $returnRequire2;
echo "<br>";
?>