简体   繁体   English

php错误无法重新声明类

[英]php error cannot redeclare class

I have three classes DatabaseHandler, MainPage, and Form 我有三个类DatabaseHandler,MainPage和Form

DatabaseHandler.php DatabaseHandler.php

class DatabaseHandler extends DatabaseConnection
{

}

MainPage.php MainPage.php

require 'DatabaseHandler.php'
class MainPage
{

}

Form.php Form.php

require 'DatabaseHandler.php'
class Form
{

}

on this util.php file it throws an error "cannot redeclare class" 在此util.php文件上,它引发错误“无法重新声明类”

//util.php
require 'MainPage.php';
require 'Form.php';

how can I avoid this error, i searched before and it says that the only solution is "namespace", i tried reading about it but Im confused and i dont know how to apply it on my situation. 我如何避免这个错误,我之前搜索过,它说唯一的解决方案是“命名空间”,我尝试阅读有关此内容的信息,但我很困惑,我不知道如何将其应用于我的情况。

The util.php requires the MainPage.php and Form.php . util.php需要MainPage.phpForm.php

Use require_once 'DatabaseHandler.php' instead of require 'DatabaseHandler.php' . 使用require_once 'DatabaseHandler.php'代替require_once 'DatabaseHandler.php' of require 'DatabaseHandler.php'

This should work! 这应该工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM