<?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>wide thoughts &#187; objects</title>
	<atom:link href="http://gasper.kozak.si/blog/tag/objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://gasper.kozak.si/blog</link>
	<description>a blog of one of those ... software developer geezers</description>
	<lastBuildDate>Sun, 22 Jan 2012 00:03:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Iterating over properties in window object</title>
		<link>http://gasper.kozak.si/blog/2009/09/06/iterating-over-properties-in-window-object/</link>
		<comments>http://gasper.kozak.si/blog/2009/09/06/iterating-over-properties-in-window-object/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 12:20:03 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[properties]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=436</guid>
		<description><![CDATA[I've been playing around a bit with JavaScript, and found out an interesting thing concerning property iteration in the <em>window</em> object in IE. In some cases, properties that are set on the window object, don't show up in the iteration.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around a bit with JavaScript, and found out an interesting thing concerning property iteration in the <em>window</em> object in IE. In some cases, properties that are set on the <em>window</em> object don&#8217;t show up in the iteration.<br />
<span id="more-436"></span><br />
As you probably know, setting a variable outside a function (in the so called global scope) actually writes to the window object.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">window.<span class="me1">X</span> = <span class="nu0">123</span>;</div>
</li>
<li class="li1">
<div class="de1">window<span class="br0">&#91;</span><span class="st0">&#39;X&#39;</span><span class="br0">&#93;</span> = <span class="nu0">123</span>;</div>
</li>
<li class="li1">
<div class="de1">X = <span class="nu0">123</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> X = <span class="nu0">123</span>;</div>
</li>
</ol>
</div>
<p>All of these do exactly the same: set a property <em>X</em> on object <em>window</em>.</p>
<p>But, when it comes to iterating window properties in IE (surprise), there seems to be a difference between the first two assignments and the last two. The code for iterating is simple; it iterates through all of the window&#8217;s properties and alerts if <em>X</em> is found.</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> v <span class="kw1">in</span> window<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>v == <span class="st0">&#39;X&#39;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span>v + <span class="st0">&#39;=&#39;</span> + window<span class="br0">&#91;</span>v<span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>With the first two assignments (the ones that write explicitly to <em>window</em>), the property shows up, but with the last two (where properties are implicitly assigned to <em>window</em>), it doesn&#8217;t. It works in Firefox, Opera, Chrome, Safari, but not in IE (not even in IE8). You can try it out <a href='http://kozak.si/widethoughts/wp-content/uploads/2009/09/obj_iter.html'>here</a>.</p>
<p>Why this is so, I can only guess. The property obviously exists, because <em>window['X']</em> and <em>window.X</em> always contain the correct value. One possibility is that iterating over object properties in IE doesn&#8217;t iterate over the properties that object actually <em>owns</em>, but rather over a separate collection of property names. This separate collection isn&#8217;t updated properly when variables are assigned to the <em>window</em> object implicitly. Another possibility is that such variables exist in a global closure, and as such aren&#8217;t true members of the <em>window</em> object. When you access <em>window.X</em> and if it doesn&#8217;t exist, it&#8217;s looked up in that closure.</p>
<p>Interesting if not life-changing stuff. Maybe I&#8217;ll do some more research on this.</p>
]]></content:encoded>
			<wfw:commentRss>http://gasper.kozak.si/blog/2009/09/06/iterating-over-properties-in-window-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>serializing objects with different class definitions</title>
		<link>http://gasper.kozak.si/blog/2009/06/25/serializing-objects-with-different-class-definition/</link>
		<comments>http://gasper.kozak.si/blog/2009/06/25/serializing-objects-with-different-class-definition/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 13:42:44 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[persistent]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[serialization]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=409</guid>
		<description><![CDATA[When it comes to (un)serializing objects in PHP, some things may surprise you. In this post I show what I&#8217;ve found out last week, when I was testing serialization with different class definitions. This is generally a bad practice, and shows one of the biggest drawbacks of using serialization for persistent object storage: the serialized [...]]]></description>
			<content:encoded><![CDATA[<p>When it comes to (un)serializing objects in PHP, some things may surprise you. In this post I show what I&#8217;ve found out last week, when I was testing serialization with different class definitions. This is generally a bad practice, and shows one of the biggest drawbacks of using serialization for persistent object storage: the serialized data holds a frozen version of an object. As project evolves, and classes change, the serialized information doesn&#8217;t change with them. When objects are unserialized with the new class definition, it can result in unexpected behavior. You should take care when using serialization for persistent or temporary storage (ie caching objects in memcache), because every change in the class definition may affect the unserialized objects, causing numerous bugs and crashes.<br />
<span id="more-409"></span><br />
Every example is following the same procedure; file <em>write.php</em> declares a <em>class X</em>, creates an instance of it, and then writes it to a file serialized with obj_write(). The second file, <em>read.php</em>, declares a <em>different class X</em>, reads the file and unserializes the object with obj_read(), which results in creating an instance of the object. Then it executes some code, such as print_r of the object, or echoes some properties.</p>
<p>Helper functions:</p>
<pre code="PHP">
function write_obj($obj)
{
  file_put_contents('obj.data', serialize($obj));
}

function read_obj()
{
  return unserialize(file_get_contents('obj.data'));
}
</pre>
<p><strong>Example 1</strong><br />
Serializing an object with a public property.</p>
<pre code="PHP">
class X
{
  public $a = 'A';
}
obj_write(new X());
</pre>
<p>read.php declares a blank class.</p>
<pre code="PHP">
class X
{
}
$x = obj_read();
print_r($x);
echo $x->a
</pre>
<p>As you might expect, the public property is restored correctly:</p>
<pre>
X Object
(
    [a] => A
)
A
</pre>
<p><strong>Example 2</strong><br />
Adding some protected properties.</p>
<pre code="PHP">
class X
{
  public $a = 'A';
  protected $b = 'B';
  protected $c = 'C';
}
obj_write(new X());
</pre>
<p>read.php declares only one protected variable $c.</p>
<pre code="PHP">
class X
{
  protected $c;
}
$x = obj_read();
print_r($x);
echo $x->a;
echo $x->b;
echo $x->c;
</pre>
<p>An this is the result:</p>
<pre>
X Object
(
    [c:protected] => C
    [a] => A
    [b:protected] => B
)
A
Notice: Undefined property: X::$b in /home/gasper/phpser/test1-read.php on line 12
Fatal error: Cannot access protected property X::$c in /home/gasper/phpser/test1-read.php on line 13
</pre>
<p>As you see, print_r correctly prints out the object. Properties $b and $c are both protected. What differs is that when printing out $x->b, PHP reports that $b is undefined property, and it correctly throws a Fatal when accessing $c. The question is, why doesn&#8217;t the fatal error already occur when accessing $b? As you can see from print_r output, property $b is present in the $x, and it&#8217;s correctly marked as protected, just as is $c. The only difference here is that $b isn&#8217;t declared in the class definition, so I guess PHP checks the class definition when accessing properties, rather than actual object information.</p>
<p><strong>Example 3</strong><br />
Now let&#8217;s twist up things some more by modifying X definition in read.php:</p>
<pre code="PHP">
class X
{
  public $c;
  function getB()
  {
    return $this->b;
  }
  function getC()
  {
    return $this->c;
  }
}
print_r($x);
echo "a: " . $x->a;
echo "b: " . $x->b;
echo "c: " . $x->c;
echo "getB: " . $x->getB();
echo "getC: " . $x->getC();
</pre>
<p>As you can see, I&#8217;ve changed $c visibility to public, and I&#8217;ve written two getters. The former is a test whether I can shift visibility of $c upon unserializing, while the second will hopefully allow me to read the variables $b and $c, which are protected in the original definition, and not declared in this one.</p>
<p>Here&#8217;s the output:</p>
<pre>
X Object
(
    [c] =>
    [a] => A
    [b:protected] => B
    [c:protected] => C
)
a: A
Notice: Undefined property: X::$b in /home/gasper/phpser/test1-read.php on line 20
b: c:
Notice: Undefined property: X::$b in /home/gasper/phpser/test1-read.php on line 9
getB:
getC:
</pre>
<p>The first strange thing is that there are two $c properties declared; one protected and one public. While this might be expected (the serialized information specifically tells PHP to unserialize a <em>protected variable $c</em>), it&#8217;s still strange that I now have <em>two</em> variables named $c. I don&#8217;t think this is possible to achieve without serialization. If you subclass a class and shift visibility of a variable from private/protected to protected/public, you still only have one single variable, so this behavior may come as unexpected. Still, the $x->c and getC() both return an empty value, because no value for <em>public $c</em> was present in the serialized object.</p>
<p>The other thing is that I still can&#8217;t access $b, even through a getter. The property is obviously present in the object (as print_r shows), but even when accessing it through a getter, which <em>has</em> access to instance&#8217;s protected variables, PHP reports that it&#8217;s undefined. I can&#8217;t think of a reasonable explanation for that, but this again shows that care should be taken when serializing objects.</p>
<p><strong>Conclusion</strong><br />
As stated before, serializing and unserializing objects with different versions of classes can be a cause for a lot of trouble. If your classes rarely change, or if you have some means of invalidating the serialized objects (ie flushing the cache, or rewriting the rows in the database), then you&#8217;re probably fine, although you should always be aware of possible consequences. Likewise, if you cache object with public properties only, these seem to work fine, whether they&#8217;re declared or not.</p>
<p>But if you have classes that change often or rely heavily on persistent storage of serialized objects, you should use another way of doing it. One way I can think of is reading the written object with the old version of the class, and passing them to another script/service, which writes them in the new format. This is possible to achieve, but is quite volatile. Other means include using XML to store persistent object data, perhaps even JSON. In these cases, you don&#8217;t store the object itself, just as with serialization, but a subset of its properties that are essential to restoring it correctly. Upon recreating the object, these properties are read one by one into a blank object of a proper class version.</p>
<p>So, that&#8217;s it. Take care with serialization! <img src='http://gasper.kozak.si/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://gasper.kozak.si/blog/2009/06/25/serializing-objects-with-different-class-definition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

