title: Call a session inside a function with object oriented php
body: <p>I'm new to oop and some lines of my code are only approximate, to indicate what I want to achieve.</p>
  
  <p>I want to create a session with php within a function within a class and call it in another function.</p>
  
  <pre><code>class My_beautiful_class
  {
  public function index()
      {
          $_SESSION['ciao'] = 'ciao';
          var_dump($_SESSION);
      }
  public function anotherfunction()
      {
          $this-&gt;index();
          var_dump($_SESSION);
      }
  }
  </code></pre>
  
  <p>I just want to understand a concept: in this way, my code will work but, on the other end, it will execute everything it will find in my function index, in my other function anotherfunction. So, if I call two variable with the same name, I could have a problem.</p>
  
  <p>I guess that in theory, I could handle the problem in another way which is that I could create a variable call sessionone for example and send it some values in with my index function:</p>
  
  <pre><code>class My_beautiful_class
  {
  
  public sessionone = [];
  
  public function index()
      {
          $_SESSION['ciao'] = 'ciao';
          $this-&gt;sessionone = $_SESSION['ciao'];
  
      }
  public function anotherfunction()
      {
          $this-&gt;sessionone;
          var_dump($_SESSION);
      }
  }
  </code></pre>
  
  <p>, but I was wondering if there any way to for example call only one variable that lives in one function with my first approach.</p>
  
  <p>Something like: (My code is wrong on purpose, it is only to indicate what I want to achieve)</p>
  
  <pre><code>public function index()
          {
              $_SESSION['ciao'] = 'ciao';
          }
  public function anotherfunction()
          {
             $this-&gt;index( $_SESSION['ciao'] );
          }
      }
  </code></pre>