简体   繁体   中英

PHP: how to access variables inside a function that have been declared outside of it?

At the very beginning of each page I include a file that starts the SESSION etc, lets call it session.php . In this file session.php , I include a file that contains a function, let's call it function1.php , because I need the function to be available in session.php .

However, later in the main page I also include function2.php which needs to access variables set in session.php , so I additionally tried to include session.php in function2.php .

The problem is that an error occurs as function1 will be declared multiple times...

Fatal error: Cannot redeclare function1() (previously declared in ...

So, what would be a more elegant and clean(er) solution for this? How could you solve it?

Basically, I'd need to access variables inside a function that have been included in the main page before...

This isn't an issue with public and private variables, since the variables you want from session.php aren't part of an object. They're just variables which you declared (probably) in the global scope, and now need to access in function2.php.

First: are the contents of function2.php... a function? If not, then that file should have the same scope as the location in your script from which it was included, and it should be able to access them just fine.

If the place from which you need to access the session.php variables is a function, or an object, you have a few choices.

0. Re-include the file

This is what you're trying to do now. It fails because session.php also contains a function definition and you can't re-define a function once it's been created. As a workaround, you could move the variable definitions into a separate file and just include that in function2. This is clumsy, but simple.

1. Pass the variables to the function when you call it.

  • Pros: simple and flexible.
  • Cons: Makes calling the function kind of a pain. You could simplify this by putting them all into a single array of parameters, rather than passing each separately.

2. Use global

At the beginning of function2, just add the command global $var1, $var2 ... to import any variables that were used elsewhere in the script.

This is also very simple, but using global variables is seen by many programmers as sloppy and opens you up to hard-to-debug errors elsewhere.

3. Use constants

In session.php, define the variables you want to share as constants. eg, define("PASSWORD", "coolpassword123") . Constants can be referenced from inside functions or objects, etc., and can never be changed after being declared. eg, echo(PASSWORD) (no quotes, no $).

Constants are generally the best solution to this kind of problem as long as 1) you don't need to change their value, and 2) you can stick to simple numeric or string values. A constant can't be an array, object, etc.

try include_once instead of include

http://php.net/manual/en/function.include-once.php

EDIT: Complete example

main.php

<?php
include_once 'session.php';
include 'function2.php';

session.php

<?php
include_once 'function1.php';
function1('session.php');

function1.php

<?php
function function1($calledFrom)
{
    echo 'function1 called from: ' . $calledFrom . PHP_EOL;
}

function2.php

include_once 'function1.php';
function1('function2.php');

Put these all in the same folder and run

php main.php

The output is:

function1 called from: session.php

function1 called from: function2.php

First make sure you aren't using the same name for functions across files that will be included.

After that, you can simply pass the variable in question as a parameter for the function

function1($variable1);

then retrieve it in the function

function function1($getvar){
    //do something with the passed variable $getvar
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM