dty63504
dty63504
2014-12-30 20:39

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

  • php

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条回答