title: Accessing PHP object variables from Javascript
body: <p>I have a PHP class object called Datapoints stored in session and I am trying to access a variable from it in a javascript function.</p>
  
  <p>From within the javascript I can access session variables through $_SESSION['foo'], but I can't access the object variables. I've tried all sorts of syntax to return the variable.</p>
  
  <p>I can access variables like this, and it works fine. </p>
  
  <pre><code>&lt;?php echo intval($_SESSION['count']); ?&gt;
  </code></pre>
  
  <p>but I would rather have the whole object in session, for example if I do this...  </p>
  
  <pre><code>&lt;?php
      header("Content-Type: text/javascript");  
      session_start();  
      $c = $_SESSION['datapoints']-&gt;count;
      $c1 = $datapoints-&gt;count;
  ?&gt;  
  var count = &lt;?php echo $c; ?&gt;
  var count1 = &lt;?php echo $c1; ?&gt;
  </code></pre>
  
  <p>Both return null.</p>
  
  <p>My object looks like this through print_r('datapoints').  </p>
  
  <pre><code>Datapoints Object ( [startdate] =&gt; 2013-06-05 [currency] =&gt; GBP [count] =&gt; 4527 [datapoints] =&gt; Array ( [0] =&gt; Datapoint Object (...) ... ))
  </code></pre>
  
  <p>Thanks</p>
  
  <p>EDIT:</p>
  
  <p>Thanks for your help, I've solved this now. Turns out it was a problem with the autoloader not loading classes properly from within the javascript files.</p>