简体   繁体   English

范围问题:在类中调用时,必需的文件无法访问变量

[英]Scope issue: required file cant access variables when called in class

$test = test;

class getFile{
    public function __construct($fileName){
    require $fileName;
  } 
}

$get = new getFile('file.php');

So file.php contains, echo $test; 所以file.php包含了,echo $ test;

If I was to call this outside the class eg 如果我要在课外叫这个,例如

require 'test.php'; 需要'test.php'; it would get $test fine But calling inside the class has obvious scope issues. 它将获得$ test罚款,但是在类内部调用存在明显的范围问题。 How can I give the files required within the function access the to variables? 如何给函数内所需的文件访问变量?

EDIT: ------------------------ 编辑:------------------------

I have multiple variables I wish this file to access (they are all dynamic based on the page, so adding x & y variables is impossible as they won't always be set) Rather than declaring each variable as globally accesible, is there no way to allow the required file in the class, access theese variables as if it wasn't? 我有多个变量希望该文件访问(它们都是基于页面的动态变量,因此不可能添加x&y变量,因为它们不会总是被设置)而不是将每个变量声明为全局可访问的,这是没有办法的允许所需的文件在类中,就像访问这些变量一样?

Thank you guys for the feedback. 谢谢你们的反馈。 Unfortunatly what I want does not seem possible, but you have inspired me to create a workaround that registers the most important variables I need my files to access as global. 不幸的是,我想要的东西似乎无法实现,但是您启发了我创建一个变通方法,该变通方法将需要我的文件才能访问的最重要变量注册为全局变量。

public function __construct($fileName) {
    global $test;
    require $fileName;
} 

will work 将工作

Perhaps you could declare a global variable within your class, like so: 也许可以在类中声明一个全局变量,如下所示:

$text = 'test';

class getFile {
   public function __construct($fileName) {
      global $test;
      require $fileName;
   }
}

$get = new getFile('file.php');

PHP variables declared in the global scope are not automatically made available inside functions or methods. 在全局范围内声明的PHP变量不会自动在函数或方法内部可用。 You have to explicitly declare them to be globals: 您必须明确声明它们为全局变量:

file.php:

    <?php
    global $test
    echo $test

or 要么

public function __construct($filename) {
    global $test;
    require $filename;
}

would both fix the problem. 都可以解决问题。 Even though the file.php script appears to be global, as there's no function definition within it, it's still bound to the scope of the __construct() method in your object, which will NOT have $test global. 即使file.php脚本看起来是全局的,但由于其中没有函数定义,它仍绑定到对象中的__construct()方法的范围,该方法不会具有$ test全局变量。

responding to your comment, i have workaround you may like 回应您的评论,我有解决方法,您可能会喜欢

class getFile{
    public $files = Array();
    public function __construct($fileName){
        $this->files[] = $fileName;
    } 
}

$get = new getFile('file.php');

foreach($get->files as $file) require($file);

or maybe u can create static methods? 或者也许你可以创建静态方法?

class getFile{
    public static $files = Array();
    public static function get($fileName){
        self::$files[] = $fileName;
    } 
}

getFile::get('file1.php');
getFile::get('file2.php');
getFile::get('file3.php');

foreach(getFile::$files as $file) require($file);

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

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