Search code examples
phpincluderequirephp-include

Failure in including files in PHP


I know there are a lot of questions related to this and I tried the solutions for my situation but none of them worked. I have to modify an API built using FlightPHP Framework (already written). There are more than 100 functions and most of them use some static variables. I'm trying to declare them in a single page so that if their value needs to be changed one day, I can do it easily by changing it at only one place.

This is the directory structure.

index.php
constants.php
FOLDER
  - firstInnerPage.php
  - secondInnerPage.php

I'll present a sample of each page.

constants.php

<?php
$member_default_cover  ='img/members/member-default.jpg';
$member_default_avatar ='img/members/member-default.jpg';
?>

These are the constants I want to use in every function.

index.php

<?php
require 'flight/Flight.php';

//Route to index page function
Flight::route('/indexPageFunction', function()
{
   pages_included();    
   $returnarray=indexPageFunction();
   header('Content-type:application/json;charset=utf-8');
   echo json_encode($returnarray);
});

//Route to first page function
Flight::route('/firstInnerPageFunction', function()
{
   pages_included();    
   $returnarray=firstInnerPageFunction();
   header('Content-type:application/json;charset=utf-8');
   echo json_encode($returnarray);
});

//Route to second page function
Flight::route('/secondInnerPageFunction', function()
{
   pages_included();    
   $returnarray=secondInnerPage();
   header('Content-type:application/json;charset=utf-8');
   echo json_encode($returnarray);
});

Flight::start();

//Calling this function includes the inner page and makes them accessible
function pages_included()
{   
  require_once 'FOLDER/firstInnerPage.php'; 
  require_once 'FOLDER/secondInnerPage.php'; 
}

//Index page function
function indexPageFunction()
{
 require_once 'constants.php';
 $avatar =  $member_default_avatar;
}

?>

For index page function, the file gets included. When a user places a request, it first reaches routes from where the request is redirected to the correct function. But the inner pages have their own function. They are written in separate pages to categorize the functions. And those pages are included in the route itself, so when a request for an inner page function comes, those files gets included. pages_included() does the job of including the inner pages.

This is how an inner page looks like.

firstInnerPage.php

<?php
function firstInnerPageFunction()
{
 require_once 'constants.php';
 //require_once '../constants.php';
 $avatar = $member_default_avatar;
}
?>

Second Inner page is also similar, so, I am not including it here.

I tried to include the file both ways: 1) require_once 'constants.php'; 2) require_once '../constants.php';

but both failed. I either get a failed to include the file error or undefined variable $member_default_avatar error.

Can anyone tell me what's the correct method to include a file in such situations? Thank you al.


Solution

  • As I said in my comment, I don't like to include php files inside functions. PHP files should always have global scope (unless you use namespaces), try to keep it simple like that.

    So, here's a code example:

    contants.inc

    <?php
    
    $myStaticVars = ['member_default_cover'  => 'img/members/member-default.jpg',
                     'member_default_avatar' => 'img/members/member-default.jpg'];
    

    firstInnerPage.php

    <?php
    
    require_once('../constants.inc');
    
    function firstInnerPageFunction()
    {
      global $myStaticVars;
      extract($myStaticVars);  // make static vars local
      $avatar = $member_default_avatar;
    }
    

    This way you are also sure that the static variables are always available, because if you use require_once on the same file in different function calls it will only work for the first function you call.

    Instead of an array you could also use define(), if your static variables are really constant. The benefit is that constants are available everywhere, also inside functions.