<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flexperiments &#187; PHP / HTML</title>
	<atom:link href="http://labs.flexperiments.nl/category/php-html/feed/" rel="self" type="application/rss+xml" />
	<link>http://labs.flexperiments.nl</link>
	<description></description>
	<lastBuildDate>Thu, 30 Dec 2010 09:41:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Storing classes in a PHP Session with AMFPHP</title>
		<link>http://labs.flexperiments.nl/storing-classes-in-a-php-session-with-amfphp/</link>
		<comments>http://labs.flexperiments.nl/storing-classes-in-a-php-session-with-amfphp/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 20:44:09 +0000</pubDate>
		<dc:creator>Ids</dc:creator>
				<category><![CDATA[PHP / HTML]]></category>
		<category><![CDATA[amfphp]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">http://labs.flexperiments.nl/?p=66</guid>
		<description><![CDATA[I&#8217;m working on a small project with a AMF / PHP backend. The project has different roles and users, and I&#8217;m storing the user object in the session of PHP. Now, according to the PHP manual, it should be possible to store complex objects (and classes) in the session. So when I put the user [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a small project with a AMF / PHP backend. The project has different roles and users, and I&#8217;m storing the user object in the session of PHP.</p>
<p>Now, according to the PHP manual, it should be possible to store complex objects (and classes) in the session. So when I put the user in the session and then dumped the session to the browser, I was pleased to see everything was in it. So far so good&#8230;</p>
<p>But then, strange things started to happen. Whilst the data was in the session, I wasn&#8217;t able to get it out easily. To understand what I mean, look at the following examples:</p>
<p>The user has a property &#8216;projects&#8217;, which is an array of projectObjects, which look like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p66code7'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p667"><td class="code" id="p66code7"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ProjectObject <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$_explicitType</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ProjectObject&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$id</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$lastModified</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Pretty straightforward.<br />
Now, when i output the projects array using print_r, I get this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p66code8'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p668"><td class="code" id="p66code8"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> __PHP_Incomplete_Class Object
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>__PHP_Incomplete_Class_Name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> ProjectObject
            <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Test1
            <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
            <span style="color: #009900;">&#91;</span>lastModified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1218479521000</span>
        <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> __PHP_Incomplete_Class Object
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>__PHP_Incomplete_Class_Name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> ProjectObject
            <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Test
            <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span>
            <span style="color: #009900;">&#91;</span>lastModified<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1218479559000</span>
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Which is ok (I think).<br />
But when I want to access any of the properties of the project, it will return an error, null or an empty string:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p66code9'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p669"><td class="code" id="p66code9"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$user</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">projects</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">id</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// returns &quot;&quot;;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$user</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">projects</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Fatal error: Cannot use object of type _PHP_Incomplete_Class as array</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$user</span> <span style="color: #339933;">-&gt;</span> <span style="color: #004000;">projects</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//Fatal error: Cannot use object of type _PHP_Incomplete_Class as array</span></pre></td></tr></table></div>

<p>Weird huh?<span id="more-66"></span><br />
The problem is the _PHP_Incomplete_Class, which indicates that the object is not properly stored. After some googling, it turns out you have to define every object / class you want to store in your session <strong>before</strong> you start the session. So, the solution should be easy, something like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p66code10'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6610"><td class="code" id="p66code10"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/objects/ProjectObject.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This should work, we define the class, and then start the session. However, somewhere in AMFPHPs internal code (in the Filters.php file to be specific), the session is already started. I could of course alter the Filters file and don&#8217;t start the session, or define my object there, but then I would have to do it for every future AMFPHP version, which is not what I want.<br />
Instead, I&#8217;ve created a Objectinitializer file, which defines every object I want to put in the session and then call session_start(). Next, I have to make sure that my call is called before the Filters file of AMFPHP is loaded, and that&#8217;s it is called somewhere outside the AMFPHP core.<br />
Well, there&#8217;s one file in the AMFPHP framework which is more or less designed to have custom code, and it&#8217;s the globals.php file.<br />
I&#8217;ve defined the servicesPath first, and then call my initializer. This code is executed before any AMFPHP core code, so our object can now be added to the session</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p66code11'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6611"><td class="code" id="p66code11"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$servicesPath</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;../projects/testProject/&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$voPath</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;../projects/testProject/objects/&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Start the session, to make sure we're the first</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$servicesPath</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;Objectinitializer.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>By the way, the Objectinitializer looks like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p66code12'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6612"><td class="code" id="p66code12"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*
 * This files defines all objects before the session is
 * started, so we put every object in the session if we
 * want. 
 */</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/objects/ProjectObject.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/objects/UserObject.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://labs.flexperiments.nl/storing-classes-in-a-php-session-with-amfphp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

