Search code examples
phpdrupal-7

php file showing output from a different file


I am very new to PHP programming. I am trying to write this custom php code in Drupal and seeing this weird behavior. Basically I have two php files which users can hit and the first php file is showing output from the second one. I am not including (include) the second file in the first one.

Home.php - The first file (outputs 'why am i executing' at the end)

<?php include 'HomeView.BL.inc';
  //Other links to access second file 
?>

HomeView.BL.inc

<?php
include 'db.inc';
include 'dao.inc'; - has a class called IDA_Map
?>

FacultyInternshipDetail.php - The second file

<?php 
include 'InternshipDetailView.BL.inc';
?>

InternshipDetailView.BL.inc

<?php
echo "why am i executing";
include 'db.inc';
include 'dao.inc';
?>

Apart from the output from the second file, I am also seeing this error - Cannot redeclare class IDA_Map.

I have read in other posts about 'include_once' but didn't expect re-declaration error since the class (IDA_Map) is declared once per request.

Thank you.


Solution

  • Flip all these to require_once.

    <?php require_once'HomeView.BL.inc';
      //Other links to access second file 
    ?>
    

    HomeView.BL.inc

    <?php
    require_once'db.inc';
    require_once'dao.inc'; - has a class called IDA_Map
    ?>
    

    FacultyInternshipDetail.php

    <?php 
    require_once'InternshipDetailView.BL.inc';
    ?>
    

    InternshipDetailView.BL.inc

    <?php
    echo "why am i executing";
    require_once'db.inc';
    require_once'dao.inc';
    ?>
    

    include breaks when you try to include the same file twice. Also look up composer and into using a php framework so you dont have to worry about any of this!