title: Unserialize Class From String PHP Not Working
body: <p>I have a custom class in PHP. If I serialize the class and save it to a text file, I can't unserialize it later in another php file. When I try to call the class functions on the object I get</p>
  
  <blockquote>
    <p>Call to a member function [functioname()] on a non-object...</p>
  </blockquote>
  
  <p>I do include the class in both php files.</p>
  
  <p>PHP File 1:</p>
  
  <pre><code>$myobject = new myclass();
  $temp = serialize($myobject);
  file_put_contents('serializetest.txt', $temp);
  </code></pre>
  
  <p>PHP File 2:</p>
  
  <pre><code>$s = file_get_contents('serializetest.txt');
  $newobject = unserialize($s);
  </code></pre>
  
  <p>Is there some reason why a serialized class would unserialize properly?</p>
  
  <p>Update</p>
  
  <p>If I create an object and use it's main function I can unserialize the unrelated saved object. The class searches for criminal cases. Even though the two objects are entirely different, once I create and use the new object I can suddenly unserialize saved past objects. i.e. The below works but if I removed the first 3 lines of code it wouldn't.</p>
  
  <pre><code>$tempcase = new Expungement();
  $tempcase-&gt;searchCase('4B02305986','Public',true,false);
  echo "Case Number 1: " . $tempcase-&gt;caseno;
  
  $s = file_get_contents('serializetest.txt');
  echo "Serialized Data: " . $s;
  $newcase = unserialize($s);
  echo "Case Number 2: " . $newcase-&gt;caseno;
  </code></pre>