Exziled
Exziled

Reputation: 493

PHP included variables not accessible in main file

I have a main.php file that I'm running certain functions through. When I include another file that just has a bunch of vars defined in it I'm unable to access those vars inside my main.php file.

Example of main.php:

class MyMain { 
    public function include_mocks() {
        return include 'mocks.php';
    }

    public function use_mocks() {
        $included = $this->include_mocks();
        if($included == true) {
            print_r($mock1); die();
        }
    }
}

Example of included file mocks.php:

<?php 

$mock1 = array(
    'key1' => 'some data',
    'key2' => 'other data'
);

This included file has several mocks in it and I want to be able to use these mocks as variables in my main.php. For some reason it throws me a 500 saying none of these are defined after I try to print_r() them after the include.

Upvotes: 2

Views: 2730

Answers (2)

arkascha
arkascha

Reputation: 42885

The issue here is that you define those functions in the scope of one method, whilst trying to access them inside the scope of another method. Those are two separate things.

To accomplish something like that you have to either declare the variable in the global scope (considered bad practice), or store it inside the objects properties.

Consider this example:

<?php
$varInGlobal = "I am defined globally";

class myClass {
    private $varInClass = "I am defined in the class";
    private $varFromFile;

    function __construct() {
        require 'includeVar.php';
        $this->varFromFile = $varInFile;

        echo "## in constructor: \n";
        var_dump($varInFile);
    }

    function showMyVars() {
        echo "** in class: \n";
        var_dump($varInClass);
        var_dump($this->varInClass);

        echo "** in global: \n";
        var_dump($varInGlobal);
        var_dump($GLOBALS['varInGlobal']);

        echo "** in file: \n";
        var_dump($varInFile);
        var_dump($GLOBALS['varInFile']);

        echo "** in property: \n";
        var_dump($this->varFromFile);
    }
}

$obj = new myClass;
$obj->showMyVars();

The included file includeVar.php:

<?php
$varInFile = "I am defined externally";

The output is:

## in constructor:
string(23) "I am defined externally"
** in class:
NULL
string(25) "I am defined in the class"
** in global:
NULL
string(21) "I am defined globally"
** in file:
NULL
NULL
** in property:
string(23) "I am defined externally"

This may not be convenient or what you expected, but this is how object orientation works. Which is why it is not a common practice to "outsource" variable declarations to separate files.

Instead you can "inject" such variables if you really have to keep them separate by handing them over to the constructor of your class. Or to a factory associated with that class.

Upvotes: 4

Salvatore Quaranta
Salvatore Quaranta

Reputation: 3

Try this

<?php
Class MyMain
{
    public function use_mocks($filename = 'mocks.php')
    {
        if(file_exists($filename))
        {
            if(include($filename))
            {
                print_r($mock1);           
            }
        }
        die;
    }
}

$asd = new MyMain();
$asd->use_mocks();

?>

file_exists() prevents e-warning when $filename doesn't exists. You can pass to this function the filename you want, if you don't the function will use the default value "mocks.php" Hope this helps :-)

Upvotes: 0

Related Questions