简体   繁体   English

PHP包含的变量无法在主文件中访问

[英]PHP included variables not accessible in main file

I have a main.php file that I'm running certain functions through. 我有一个main.php文件,通过它可以运行某些功能。 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. 当我包含另一个只定义了一堆变量的文件时,我无法访问main.php文件中的那些变量。

Example of main.php: 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: 包含文件的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. 这个包含的文件中包含几个模拟,我希望能够将这些模拟用作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. 出于某种原因,它抛出500,表示在包含之后我尝试对它们进行print_r()处理后,这些都没有定义。

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 : 包含的文件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. 或与该类相关的工厂。

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. 当$ filename不存在时,file_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 :-) 您可以将所需的文件名传递给该函数,如果不使用该函数,则将使用默认值“ mocks.php”。希望这会有所帮助:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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