PHP全局变量没有改变

时间:2015-06-14 15:52:19

标签: php variables global

我遇到PHP和全局问题。 在这段代码中:

  1. 在页面开始时,结果为:Test 123
  2. 点击后:ListBox1点击结果为:XXXXXXXX
  3. 点击后:ListBox2Click结果是:Test 123这是错误的!
  4. 有什么方法可以用这种方式改变全局变量吗?需要从内部进行更改" ListBox1Click"功能和显示来自以下代码:" ListBox2Click"功能

    <?php
      require_once("rpcl/rpcl.inc.php");
      //Includes
      use_unit("forms.inc.php");
      use_unit("extctrls.inc.php");
      use_unit("stdctrls.inc.php");
    
      //Class definition
    
      class Page1 extends Page
      {
        public $Label8 = null;
        global $someVar;
        $someVar = "Test 123";
        $this->Label8->Caption = $someVar;
    
        function ListBox1Click($sender, $params)
        {
          global $someVar;
          $someVar = "XXXXXXXX";
          $this->Label8->Caption = $someVar;
        }
        function ListBox2Click($sender, $params)
        {
          global $someVar;
          $this->Label8->Caption = $someVar;
        }
      }
    
      global $application;
    
      global $Page1;
    
      //Creates the form
      $Page1=new Page1($application);
    
      //Read from resource file
      $Page1->loadResource(__FILE__);
    
      //Shows the form
      $Page1->show();
    
    ?>
    

    感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您的解决方案可能如下所示:

<?php
  require_once("rpcl/rpcl.inc.php");
  //Includes
  use_unit("forms.inc.php");
  use_unit("extctrls.inc.php");
  use_unit("stdctrls.inc.php");

  //Class definition

  class Page1 extends Page
  {
    public $Label8 = null;
    private $someVar;

    public function __construct($application)
    {
        parent::__construct($application);

        //load from storage
        $this->someVar = $_SESSION['someVar'];
        $this->Label8->Caption = $this->someVar;


    }

    public function __destruct()
    {
          //save to storage
          $_SESSION['someVar'] = $this->someVar;
    }

    public function ListBox1Click($sender, $params)
    {
      $this->someVar = "XXXXXXXX";
      $this->Label8->Caption = $this->someVar;

    }

    public function ListBox2Click($sender, $params)
    {
      $this->Label8->Caption = $this->someVar;
    }
  }

  global $application;

  //Creates the form
  $Page1=new Page1($application);

  //Read from resource file
  $Page1->loadResource(__FILE__);

  //Shows the form
  $Page1->show();

?>