title: How to access a PHP variable from one function in another function
body: <p>I have already written an application in a procedural way and am trying to move into into a Laravel framework.  I'm having trouble with the SOAP exchange section as I am getting an ID value that authenticates the user but cannot access that value (as a cookie) later in the program to authenticate the search.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>&lt;?php namespace App;
  
  use Artisaninweb\SoapWrapper\Facades\SoapWrapper;
  use Illuminate\Http\RedirectResponse;
  
  class SoapController {
  
  private $auth_response;
  private $cookie;
  private $search_client;
  private $search_response;
  
  public function soapExchange() {
  
      // create SOAP client and add service details
      SoapWrapper::add(function ($service) {
  
          $service
              -&gt;name('WoSAuthenticate')
              -&gt;wsdl('http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl')
              -&gt;trace(true)
              -&gt;cache(WSDL_CACHE_NONE);
      });
  
      SoapWrapper::service('WoSAuthenticate', function($service) {
          // call authenticate() method to get SID cookie
          $auth_response = $service-&gt;call('authenticate', []);
          $cookie = $auth_response-&gt;return;
          // test for cookie return
          // print($cookie);
      });
  
      // create SOAP client and add service details
      $search_client = new SoapWrapper;
      $search_client::add(function ($service) {
  
          $service
              -&gt;name('WoSSearch')
              -&gt;wsdl('http://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl')
              -&gt;trace(true)
              -&gt;cache(WSDL_CACHE_NONE);
      });
  
      if (isset($auth_response-&gt;return)) {
  
          // if there is an SID returned then add it to the cookie attribute of the search client
          $search_client-&gt;__setCookie('SID', $cookie);
      } else {
          // route to relevant view to display throttle error
          return redirect('throttle');
      }
  }
  }
  </code></pre>
  
  <p>I am successfully retrieving the response from the Web API call and getting a code to authenticate the user, saved as <code>$cookie</code>.  However, I need then to create another <code>SoapWrapper</code> for performing the search and this needs the ID code attached by using the <code>__setCookie</code> method.  If nothing is returned by the <code>authenticate</code> call then it redirects to an error message via <code>throttle.blade.php</code> elsewhere.</p>
  
  <p>Surely there is a way to return a value created from a function so that it can be used elsewhere?</p>
  
  <p>** EDIT **</p>
  
  <p>Looked into employing <code>SoapClient</code> instead and including all operations within a single function.  It all relates to a specific Web API anyway so I guess separation of concerns is not so much of an issue.  FYI the new class I am trying is this:</p>
  
  <pre><code>&lt;?php namespace App\Models;
  
  use SoapClient;
  use Illuminate\Http\RedirectResponse;
  
  class SoapWrapper {
  
  public function soapExchange() {
  
      // set WSDL for authentication and create new SOAP client
      $auth_url  = "http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl";
  
      // array options are temporary and used to track request &amp; response data
      $auth_client = @new SoapClient($auth_url);
  
      // set WSDL for search and create new SOAP client
      $search_url = "http://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl";
  
      // array options are temporary and used to track request &amp; response data
      $search_client = @new SoapClient($search_url);
  
      // run 'authenticate' method and store as variable
      $auth_response = $auth_client-&gt;authenticate();
  
      // call 'setCookie' method on '$search_client' storing SID (Session ID) as the response (value) given from the 'authenticate' method
      // check if an SID has been set, if not it means Throttle server has stopped the query, therefore display error message
      if (isset($auth_response-&gt;return)) {
          $search_client-&gt;__setCookie('SID',$auth_response-&gt;return);
      } else {
          return Redirect::route('throttle');
      }
  }
  }    
  </code></pre>