Search code examples
phpincluderequire

Cannot require two classes in php


I am currently working on a little website and i am trying to require two classes. When I only require one class, everything works fine but when I require two classes my page is not working any more.

This code works:

<?php
require '../assets/php/mapManager.php';

$mapManager = new mapManager();
?>

This code works:

<?php
require '../assets/php/accountManager.php';

$accountManager = new accountManager();
?>

This code works not:

<?php
require '../assets/php/mapManager.php';
require '../assets/php/accountManager.php';

$mapManager = new mapManager();
$accountManager = new accountManager();
?>

Solution

  • My hypothesis is that each of those class files includes or requires the same file containing a parent Manager class or some other common dependency, and you're getting a fatal error when you try to redeclare the class in that file. You can fix this by using require_once instead of require, or ideally by implementing autoloading.

    Normally I wouldn't post a "guess" answer, but based on the context and naming and the way the error is occurring I think this is extremely likely, and if it doesn't happen to be the problem in your case, it definitely will be for someone else with the same question.