dty63504 2014-12-30 20:39
浏览 6
已采纳

为什么我得到“无法重新声明课程”错误?

I have Apache running on port 81. My project folder is MyPhpProject. Inside it I have 2 folders: Domain and Testing.

In Domain folder I have 3 PHP files:

  1. BaseDomain.php which contains an abstract class BaseDomain
  2. Location.php which contains a concrete class Location inherited from BaseDomain
  3. Employee.php which contains a concrete class Employee inherited from BaseDomain

Employee class has a reference of Location class.

This is the BaseDomain.php:

<?php
abstract class BaseDomain {

}
?>

This is the 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;
    }
}
?>

This is 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;
    }
}
?>

Now in the Testing folder I created a Test_Employee.php and this is its code:

<?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>";
?>

When I try to run it http://localhost:81/MyPhpProject/Testing/Test_Employee.php I got a fatal error regarding cannot redeclare BaseDomain class. This is what I see in browser:

getIncludePath = ../Domain

returnRequire for Location.php = 1

Fatal error: Cannot redeclare class BaseDomain in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\MyPhpProject\Domain\BaseDomain.php on line 2

I have not created BaseDomain class more than once. So this error is bizarre. Can somebody please explain why I am getting error message? And how to fix it.

Thanks for your time.

  • 写回答

2条回答 默认 最新

  • dongyuans61046 2014-12-30 20:55
    关注

    The line $returnRequire1 = require 'Location.php'; loads Location.php, which in turns loads BaseDomain.php in the line $returnRequire = require 'BaseDomain.php';. Then, the line $returnRequire2 = require 'Employee.php'; loads Employee.php, which loads (again) BaseDomain.php (the line $returnRequire = require 'BaseDomain.php';). The second load of BaseDomain.php causes php to try to redefine the BaseDomain class, which is no allowed.

    The easiest way to solve this problem is to change your require calls to require_once. This will ensure that each file is loaded exactly once per run, which will prevent the error you are experiencing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办