致命错误:无法重新声明类(包括example.php)


PHP Fatal error: Cannot redeclare class (include example.php)

我需要在整个代码中多次包含对另一个php文件example.php的引用,但我得到" php致命错误:不能重新声明类错误"。

<?php include 'example.php'; ?>

如何做到这一点?

改为:

<?php include_once 'example.php'; ?>

如果您的example.php包含一个类和其他代码,将其分离到一个文件中用于类,另一个文件用于您想要多次执行的代码。然后在调用上述代码的页面中,可以这样写:

<?php include_once 'exampleClass.php'; ?>
<?php include 'exampleCode.php'; ?>
<?php include 'exampleCode.php'; ?>