0

I have a list of files that I require_once at the beginning of my main scripts.

In one of those files, I have a bunch of functions.
In another one of those files, I have a bunch of constants (which are variables at the moment, but they are essentially constants).

Main.php

require_once \Constants.php
require_once \Functions.php

From within main.php, I am able to call functions and call the constants from their respective scripts... I am also able to call functions and pass the constants from Constants.php as parameters to the functions.
However, I am not able to use functions that have the constants embedded within them.
In other words, I cannot use the functions from Functions.php that already have the variables from the Constants.php file within the functions, but I am able to use functions from Functions.php if they do not have any variables from other included files within the function.

Function FirstFunction(){ echo $Constant1; return 1 }

FirstFunction() uses $Constant1 from Constants.php and does not work.

Function SecondFunction($param){ echo $param; return 1 }

SecondFunction() can be passed $Constant1 from Constants.php as a parameter and it works fine.

Questions:

Is there a way for me be able to use my Main.php to call a function file and a constant file and have the function file use variables from the constant file without explicitly calling or passing them from within Main.php?

If I were to daisy chain them (Main.php calls Functions.php; Functions.php calls Constants.php) will this work? (I kind of tried this but not well enough to either trust or rule out this method just yet).

Note

Most of the information I am able to find is regarding using variables from included files, but not specifically about included files using variables from other included files.

2
  • Not sure what the problem is here, as you haven't posted any code examples of said functions that doesn't work. However, I have a sneaking suspicion that this is because of the constants being define inside the functions..? Commented Jul 14, 2015 at 16:26
  • yes, that is exactly what I am asking about. I have functions that use the variables/constants from another included file within (or inside) the functions themselves. I can post a generic example if you require one to visualize it. Commented Jul 14, 2015 at 19:19

2 Answers 2

1

This sounds like a simple scope issue, that has nothing to do with your include-cycle.

When you create your 'constants' as normal PHP-variables ($name='value';), in the root scope of the Constants.php, then they must be called inside any later functions, by referencing them first ... like this:

$name='value';

    /** other stuff **/

function Foo(){
    global $name;
    //You have access to $name here
}

The true solution tho, is to actually define your constants as real constants, which makes them available in any scope ... like this:

define('NAME','value');

    /** other stuff **/

function Foo(){
    //You immediately have access to NAME here
}

Only downside to true constants is, that they are constant ... meaning, they can’t be changed at a later point in your script.

You can read more about 'scope' here: http://php.net/manual/en/language.variables.scope.php

Sign up to request clarification or add additional context in comments.

Comments

0

include() works as if the contents of the file being included are literally cut/paste into the spot where the include() call is:

variables.php:

<?php
$foo = 'bar';

test.php:

<?php
var_dump($foo); // notice: undefined variable
include('variables.php');
var_dump($foo); // string(3) "bar"

Note that standard PHP variable scoping rules apply. So if you do the include() inside a function:

<?php

var_dump($foo); // notice: undefined variable
function test() {
   var_dump($foo); // notice: undefined variable
   include('variables.php');
   var_dump($foo); // string(3) "bar"
}
test(); // call the test function
var_dump($foo); // notice: undefined variable

notice how $foo doesn't exist before/after the function is defined/called - it will only exist WITHIN the function, and only AFTER it's been included().

If you want your standard variables to be truly global, you'll have to include the file at a global scope, or declare them global:

variables.php:

<?php
global $foo;
$foo = 'bar';

and then using the same script, $foo will "magically" appear for the final var_dump, because it's been made global.

1 Comment

Hello, thanks for the answer. As stated in my OP, "I have a list of files that I require_once at the beginning of my main scripts." require_once \Constants.php require_once \Functions.php as you can see, Constants.php or the file with all my variables is included first, and then my file full of functions. I believe there is no issue regarding when or where I do my includes. But it does add more relevant information for a more complete answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.