Serializing a plain PHP class to an associative array
<p>Consider a simple class:</p>

<pre><code>class Token{
    private $hash = "";
    private $userId = "";

    public function  __construct($hash, $userId) {
        $this-&gt;hash = $hash;
        $this-&gt;userId = $userId;
    }

    public function getHash() {
        return $this-&gt;hash;
    }

    public function getUserId() {
        return $this-&gt;userId;
    }

    public function setHash($hash) {
        $this-&gt;hash = $hash;
    }

    public function setUserId($userId) {
        $this-&gt;userId = $userId;
    }
}
</code></pre>

<p>Trying to serialize it to an associative array, like so:</p>

<pre><code>// the token
$t = new Token("sampleHash", "sampleUser");

// an array for comparison
$retObj = array();
$retObj['hash']   = $t-&gt;getHash();
$retObj['userId'] = $t-&gt;getUserId();

print_r((array) $t);
echo "&lt;br&gt;";
print_r($retObj);
</code></pre>

<p>I get this:</p>

<pre><code>Array ( [�Token�hash] =&gt; sampleHash [�Token�userId] =&gt; sampleUser )
Array ( [hash] =&gt; sampleHash [userId] =&gt; sampleUser )
</code></pre>

<p>What's going on? How do I fix the serialization to look like the second print line?</p>
