类内的 PHP 全局变量作用域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8488407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:51:04  来源:igfitidea点击:

PHP global variable scope inside a class

phpclassvariablesscopeglobal

提问by roamcel

I have the following script

我有以下脚本

myclass.php

我的类.php

<?php

$myarray = array('firstval','secondval');

class littleclass {
  private $myvalue;

  public function __construct() {
    $myvalue = "INIT!";
  }

  public function setvalue() {
    $myvalue = $myarray[0];   //ERROR: $myarray does not exist inside the class
  }
}

?>

Is there a way to make $myarray available inside the littleclass, through simple declaration? I don't want to pass it as a parameter to the constructor if that was possible.

有没有办法通过简单的声明使 $myarray 在 littleclass 中可用?如果可能的话,我不想将它作为参数传递给构造函数。

Additionally, I hope that you actually CAN make global variables visible to a php class in some manner, but this is my first time facing the problem so I really don't know.

此外,我希望您实际上可以以某种方式使全局变量对 php 类可见,但这是我第一次遇到这个问题,所以我真的不知道。

回答by Nick Shvelidze

include global $myarrayat the start of setvalue()function.

包括global $myarraysetvalue()函数的开头。

public function setvalue() {
    global $myarray;
    $myvalue = $myarray[0];
}

UPDATE:
As noted in the comments, this is bad practice and should be avoided.
A better solution would be this: https://stackoverflow.com/a/17094513/3407923.

更新:
如评论中所述,这是不好的做法,应该避免。
更好的解决方案是:https: //stackoverflow.com/a/17094513/3407923

回答by macjohn

in a class you can use any global variable with $GLOBALS['varName'];

在一个类中,您可以使用任何全局变量 $GLOBALS['varName'];

回答by jbrtrnd

Construct a new singleton class used to store and access variables you want to use ?

构造一个新的单例类,用于存储和访问要使用的变量?

回答by Amelia

Why dont you just use the getter and setter for this?

为什么不为此使用 getter 和 setter?

<?php

    $oLittleclass = new littleclass ;
    $oLittleclass->myarray =  array('firstval','secondval');

    echo "firstval: " . $oLittleclass->firstval . " secondval: " . $oLittleclass->secondval ;

    class littleclass 
    {
      private $myvalue ;
      private $aMyarray ;

      public function __construct() {
        $myvalue = "INIT!";
      }

      public function __set( $key, $value )
      {
        switch( $key )
        {
          case "myarray" :
            $this->aMyarray = $value ;
          break ;
        }
      }

       public function __get( $key )
       {
          switch( $key )
          {
            case "firstval" :
              return $this->aMyarray[0] ;
            break ;
            case "secondval" :
              return $this->aMyarray[1] ;
            break ;
          }    
       }   
    }

    ?>

回答by djot

 $GLOBALS['myarray'] =  array('firstval','secondval');

In the class you just might use $GLOBALS['myarray'].

在课堂上,您可能只使用 $GLOBALS['myarray']。