-1

I have problem with PHP and globals. In this code:

  1. at the page starts, the result is: Test 123.
  2. After click in: ListBox1Click the result is: XXXXXXXX.
  3. After click in: ListBox2Click the result is: Test 123 which is wrong!

Is there any method where I can change global variable in this way? It's need to be change from inside "ListBox1Click" function and displays from code in: "ListBox2Click" function.

<?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();

?>

Thanks for help

wcale
  • 51
  • 9
  • 1
    Comment: You have an extra closing bracket `}` – Logan Wayne Jun 15 '15 at 02:12
  • 2
    Don't use globals. Ever. Especially in an OO scenario where encapsulation is important. Argument lists exist for a reason. Use them. – Major Productions Jun 15 '15 at 02:17
  • Thanks for advice, but the problem is that I need to store somehow few variables, those variables needs to be accessible from inside both listBoxes OnClick event functions. But right now, as the example above, after clicking on listBox2, the $someVar is changing into "Test 123" and not stays as "XXXXXXXX" – wcale Jun 15 '15 at 06:35
  • 2
    @wcale, I think what you want to achieve is a storage solution (the variable is stored between the two requests) and therefore hasn't something to do with globals. your variable `$somevar` could also be private and accessed through: `$this->somevar`... – Raphael Müller Jun 15 '15 at 09:32
  • @wcale please describe what you expect from global variables behaviour? It looks like you are expecting their values to be passed between independent http requests. That does not work this way. If you want to pass values between http requests please refer to sessions documentation. – Alexey Eremikhin Jun 15 '15 at 09:37

1 Answers1

1

Your solution could look like this:

<?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();

?>
Raphael Müller
  • 2,180
  • 2
  • 15
  • 20
  • Thanks ! It's working now. I didn't know, that there is needed $_SESSION !. And thanks for code src ! – wcale Jun 15 '15 at 14:22