如何修复 - 致命错误:无法声明类,因为该名称已在 C:\xampp\htdocs\TaskN\classes



我有父抽象类 - Main.php,它由 3 个子类扩展。

但是当我实例化子对象时,我收到此错误:

致命错误:无法声明类 DB,因为名称已在 在 C:\xampp\htdocs\TaskN\classes\DB.php 第 4 行中使用

请帮忙,我该如何解决这个问题?

主.php

<?php
include "classes/DB.php";
abstract class Main
{
public $table = 'products';
private $barcode;
private $name;
private $price;
private $image;
protected $size;
protected $height;
protected $width;
protected $length;
protected $weight;
// SET Parametres
public function setBarcode($barcode)
{
$this->barcode = $barcode;
}
public function setName($name)
{
$this->name = $name;
}
public function setPrice($price)
{
$this->price = $price;
}
public function setImage($image)
{
$this->image = $image;
}
abstract function setSize($size);
abstract function setHeight($height);
abstract function setWidth($width);
abstract function setLength($length);
abstract function setWeight($weight);
// Create Data
public function insert()
{
$sql = "INSERT INTO $this->table(barcode, name, price, size, height, width, length, weight, image)VALUES(:barcode, :name, :price, :size, :height, :width, :length, :weight, :image)";
$stmt = DB::prepare($sql);
$stmt->bindParam(':barcode', $this->barcode);
$stmt->bindParam(':name', $this->name);
$stmt->bindParam(':price', $this->price);
$stmt->bindParam(':size', $this->size);
$stmt->bindParam(':height', $this->height);
$stmt->bindParam(':width', $this->width);
$stmt->bindParam(':length', $this->length);
$stmt->bindParam(':weight', $this->weight);
$stmt->bindParam(':image', $this->image);
return $stmt->execute();
}
// Read Data
public function readAll()
{
$sql = "SELECT * FROM $this->table";
$stmt = DB::prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
// Delete Data
public function delete(array $id)
{
$placeholders = trim(str_repeat('?,', count($id)), ',');
$sql = "DELETE FROM $this->table WHERE id IN ($placeholders)";
$stmt = DB::prepare($sql);
return $stmt->execute($id);
}
}
?>

书.php

<?php
include "classes/Main.php";
class Book extends Main
{
// SET Parametre
public function setWeight($weight)
{
$this->weight = $weight;
}
public function setSize($size){}

public function setHeight($height){}

public function setWidth($width){}

public function setLength($length){}

}
?>

磁盘.php

<?php
include "classes/Main.php";
class Disk extends Main
{
// SET Parametre
public function setSize($size)
{
$this->size = $size;
}
public function setWeight($weight){}
public function setHeight($height){}
public function setWidth($width){}
public function setLength($length){}
}
?>

家具.php

<?php
include "classes/Main.php";
class Furniture extends Main
{
// SET Parametre
public function setHeight($height)
{
$this->height = $height;
}
public function setWidth($width)
{
$this->width = $width;
}
public function setLength($length)
{
$this->length = $length;
}
public function setWeight($weight) {}

public function setSize($size) {}

}
?>

不要像这样使用include,它不是 C,也没有包含保护。

首先应该require_once它,因为包含同一个文件两次(最好尽早获得错误(或在找不到文件时继续是没有意义的。

并考虑使用 PSR 自动加载约定和作曲家来避免此类问题。

最新更新