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

如何修复-致命错误:无法声明类,因为该名称已在C:\xampp\htdocs\TaskN\classes\DB.php中使用?,php,Php,我有一个父抽象类-Main.php,它由3个子类扩展 但是当我实例化子对象时,我得到了以下错误: 致命错误:无法声明类DB,因为名称已在中 在第4行的C:\xampp\htdocs\TaskN\classes\DB.php中使用 请帮助,我如何解决此问题 Main.php <?php include "classes/DB.php"; abstract class Main { public $table = 'products'; private

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

但是当我实例化子对象时,我得到了以下错误:

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

请帮助,我如何解决此问题

Main.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
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
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
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) {}
   
}

?>

Book.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
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
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
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) {}
   
}

?>

Disk.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
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
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
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) {}
   
}

?>

Furniture.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
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
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
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) {}
   
}

?>

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

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


并考虑使用PSR自动播放协议和作曲家来避免这样的问题。

请帮助我,这是我最后一个问题。如果我解决了这个问题,我将得到我的第一份工作..在你的Db.php文件中有一个同名的类,你想初始化它