<?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; PHP</title>
	<atom:link href="http://kozak.si/widethoughts/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://kozak.si/widethoughts</link>
	<description>a blog of one of those ... software developer creatures</description>
	<lastBuildDate>Sat, 31 Jul 2010 12:30:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Talk: Večjezičnost v spletnih aplikacijah</title>
		<link>http://kozak.si/widethoughts/2010/07/31/talk-vecjezicnost-v-spletnih-aplikacijah/</link>
		<comments>http://kozak.si/widethoughts/2010/07/31/talk-vecjezicnost-v-spletnih-aplikacijah/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 12:27:53 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Talks]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[L10n]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[php konferenca]]></category>
		<category><![CDATA[talk]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=585</guid>
		<description><![CDATA[I just published the slides from my &#60;?php konferenca 2010 talk. You can download them here.
]]></description>
			<content:encoded><![CDATA[<p>I just published the slides from my &lt;?php konferenca 2010 talk. You can download them <a href="/widethoughts/talks">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2010/07/31/talk-vecjezicnost-v-spletnih-aplikacijah/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pushing PDO</title>
		<link>http://kozak.si/widethoughts/2009/10/25/pushing-pdo/</link>
		<comments>http://kozak.si/widethoughts/2009/10/25/pushing-pdo/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 19:59:39 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[PDO]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=541</guid>
		<description><![CDATA[I&#8217;m really glad to see Lukas planning to push PDO a step higher. I think it&#8217;s already a decent extension, and has a bright future &#8212; since I first used the extension I&#8217;ve never ever used anything else for any database. It&#8217;s object-oriented, has a lean interface, it&#8217;s fast, and supports a major feature: prepared [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m really glad to see <a href="http://pooteeweet.org/blog/1565/p/1">Lukas planning to push PDO a step higher</a>. I think it&#8217;s already a decent extension, and has a bright future &#8212; since I first used the extension I&#8217;ve never ever used anything else for any database. It&#8217;s object-oriented, has a lean interface, it&#8217;s fast, and supports a major feature: prepared statements. I&#8217;ve completely forgotten about having to escape, because I always insert/update through these. Seeing mysql_real_escape_string() anywhere in code makes me sad, and seeing $db = mysql_connect() and then passing the resource around makes me sick <img src='http://kozak.si/widethoughts/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Yes, you can use a class to wrap this up, but why would you? PDO is just that and more.</p>
<p>The only thing I miss is some sort of prepared statement inspection. You prepare it, and you pass the parameters, but you can&#8217;t find out what the query actually looks like. This would come in handy for logging, but it may be impossible to implement in the extension, because as I understand prepared statements are assembled by the database itself, unless they&#8217;re emulated by the extension.</p>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2009/10/25/pushing-pdo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>wideimage problems?</title>
		<link>http://kozak.si/widethoughts/2009/09/19/wideimage-problems/</link>
		<comments>http://kozak.si/widethoughts/2009/09/19/wideimage-problems/#comments</comments>
		<pubDate>Sat, 19 Sep 2009 18:33:15 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[WideImage]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=526</guid>
		<description><![CDATA[Just looked at search terms leading people to my blog and found a few of &#8220;wideimage problem php&#8221;. Just to let you know, there are several support channels available if you get stuck using the library or find a bug. So, please, don&#8217;t let me guess what your problems are.  
]]></description>
			<content:encoded><![CDATA[<p>Just looked at search terms leading people to my blog and found a few of &#8220;wideimage problem php&#8221;. Just to let you know, there are <a href="http://wideimage.sourceforge.net/support/">several support channels available</a> if you get stuck using the library or find a bug. So, please, don&#8217;t let me guess what your problems are. <img src='http://kozak.si/widethoughts/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2009/09/19/wideimage-problems/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WideImage: new release</title>
		<link>http://kozak.si/widethoughts/2009/09/04/wideimage-new-release/</link>
		<comments>http://kozak.si/widethoughts/2009/09/04/wideimage-new-release/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 18:10:04 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[WideImage]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=478</guid>
		<description><![CDATA[It&#8217;s friday evening and I&#8217;ve just released a new version of WideImage. I&#8217;m such a geek.
]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s friday evening and I&#8217;ve just <a href="http://wideimage.sourceforge.net/2009/09/wideimage-9-09-04-released/">released a new version of WideImage</a>. I&#8217;m such a geek.</p>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2009/09/04/wideimage-new-release/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introducing LayerCache</title>
		<link>http://kozak.si/widethoughts/2009/08/26/introducing-layercache/</link>
		<comments>http://kozak.si/widethoughts/2009/08/26/introducing-layercache/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 17:52:17 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[LayerCache]]></category>
		<category><![CDATA[APC]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[open-source]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=464</guid>
		<description><![CDATA[I&#8217;ve recently started a new project, called LayerCache. It&#8217;s an easy-to-use caching framework for PHP5, which allows you to cache items in multiple layers. When a requested item isn&#8217;t present in one layer, the framework reads from the next layer in the stack. If the item isn&#8217;t present in any layer, it&#8217;s retrieved from the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently started a new project, called LayerCache. It&#8217;s an easy-to-use caching framework for PHP5, which allows you to cache items in multiple layers. When a requested item isn&#8217;t present in one layer, the framework reads from the next layer in the stack. If the item isn&#8217;t present in any layer, it&#8217;s retrieved from the source and stored in all caches in the stack.</p>
<p>An example of usage would be caching user profiles in Memcache (bigger, slower, global to all webservers) and APC (smaller, faster, local to each webserver). It also offers a mechanism called <em>prefetch</em>, which aims to reduce the database hit when an item is removed from the cache because of age (ttl).</p>
<p>Currently, the framework offers interfaces to Memcache, APC, file caching and local caching (PHP array with LRU, caching in a request scope). I&#8217;ll probably add more caching interfaces support as the project evolves. There is no documentation, apart from examples (see link bellow) and unit tests (yes, that counts as documentation). The current code is fully tested with PHPUnit, but no stable package is currently available.</p>
<p>For more, visit the following links:</p>
<ul>
<li><a href="http://code.google.com/p/layercache/">Project web site</a></li>
<li><a href="http://code.google.com/p/layercache/wiki/Introduction">Introduction</a></li>
<li><a href="http://code.google.com/p/layercache/wiki/Examples">Examples</a></li>
<li><a href="http://code.google.com/p/layercache/source/checkout">Browse SVN</a></li>
<li><a href="https://www.ohloh.net/p/layercache">LayerCache Ohloh profile</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2009/08/26/introducing-layercache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php&#124;architect&#8217;s rundown of frameworks fail</title>
		<link>http://kozak.si/widethoughts/2009/07/10/phparchitects-rundown-of-frameworks-fail/</link>
		<comments>http://kozak.si/widethoughts/2009/07/10/phparchitects-rundown-of-frameworks-fail/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 20:56:39 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[php|architect]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=443</guid>
		<description><![CDATA[For some reason php&#124;architect&#8217;s June issue failed to list Symfony among 6 frameworks that are supposedly worth mentioning. I&#8217;m not going to speculate on why, but I will say that it&#8217;s wrong from at least one perspective.
If a serious magazine tries to present itself as a valuable source of PHP information, and half of the [...]]]></description>
			<content:encoded><![CDATA[<p>For some reason <a href="http://www.phparch.com/magazine/index/99">php|architect&#8217;s June issue</a> failed to list Symfony among 6 frameworks that are supposedly worth mentioning. I&#8217;m not going to speculate on <em>why</em>, but I <em>will</em> say that it&#8217;s wrong from at least one perspective.</p>
<p>If a serious magazine tries to present itself as a valuable source of PHP information, and half of the issue is dedicated to frameworks, <em>and</em> they put a big caption saying &#8220;PHP HAS BEEN FRAMED &#8211; A rundown of popular frameworks&#8221; on the front cover, but then at least two of the frameworks listed are far from maturity (and userbase, and documentation) of the big players, and you fail to list at least one of the most popular frameworks, then it&#8217;s a fail. Whether it be because they couldn&#8217;t find anybody that would write an article, or they didn&#8217;t try hard enough to find anybody, or they just didn&#8217;t think about it, it&#8217;s still a fail. They <em>should&#8217;ve</em>. And the same could be said for a few other PHP frameworks.</p>
<p>I wonder what were the criteria for the framework selection. Popularity couldn&#8217;t be the only one, because that would mean at least three frameworks replaced by others.</p>
<p>Even though, it&#8217;s an interesting issue, framework articles included.</p>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2009/07/10/phparchitects-rundown-of-frameworks-fail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>serializing objects with different class definitions</title>
		<link>http://kozak.si/widethoughts/2009/06/25/serializing-objects-with-different-class-definition/</link>
		<comments>http://kozak.si/widethoughts/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://kozak.si/widethoughts/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2009/06/25/serializing-objects-with-different-class-definition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Talk: PHP in varnost</title>
		<link>http://kozak.si/widethoughts/2009/06/07/talk-php-in-varnost/</link>
		<comments>http://kozak.si/widethoughts/2009/06/07/talk-php-in-varnost/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 11:45:41 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[Talks]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[php konferenca]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[talk]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=342</guid>
		<description><![CDATA[I just published the slides and accompanying files for my &#60;?php konferenca 2009 talk. You can download them here.
Update: video of my talk is available. And also other videos from the conference. All in Slovene.
]]></description>
			<content:encoded><![CDATA[<p>I just published the slides and accompanying files for my &lt;?php konferenca 2009 talk. You can download them <a href="http://kozak.si/widethoughts/talks/">here</a>.</p>
<p>Update: <a href="http://videolectures.net/phpkonferenca09_kozak_vip/">video of my talk</a> is available. And also <a href="http://videolectures.net/phpkonferenca09_ljubljana/">other videos</a> from the conference. All in Slovene.</p>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2009/06/07/talk-php-in-varnost/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Slovenian PHP conference</title>
		<link>http://kozak.si/widethoughts/2009/05/12/slovenian-php-conference/</link>
		<comments>http://kozak.si/widethoughts/2009/05/12/slovenian-php-conference/#comments</comments>
		<pubDate>Tue, 12 May 2009 21:59:18 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[Talks]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[php konferenca]]></category>
		<category><![CDATA[talk]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=316</guid>
		<description><![CDATA[Getting ready for &#60;?php konferenca, the Slovenian PHP conference. I&#8217;ll try to shed some light on security issues (and why they are bad), and on how to properly secure a web site (and why that&#8217;s good). The workshop is wonderfully imaginatively titled PHP and security.
]]></description>
			<content:encoded><![CDATA[<p>Getting ready for <a href="http://phpkonferenca.si/">&lt;?php konferenca</a>, the Slovenian PHP conference. I&#8217;ll try to shed some light on security issues (and why they are bad), and on how to properly secure a web site (and why that&#8217;s good). The workshop is wonderfully imaginatively titled <em>PHP and security</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2009/05/12/slovenian-php-conference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoid micro-optimizations</title>
		<link>http://kozak.si/widethoughts/2009/03/10/avoid-micro-optimizations/</link>
		<comments>http://kozak.si/widethoughts/2009/03/10/avoid-micro-optimizations/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 22:25:27 +0000</pubDate>
		<dc:creator>Gašper</dc:creator>
				<category><![CDATA[Thoughts]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://kozak.si/widethoughts/?p=241</guid>
		<description><![CDATA[I&#8217;ve read this post about micro-optimization today, and I think many of the listed hints are wrong. Not wrong in way of being false, but in a way that you shouldn&#8217;t use them. Sebastian already wrote something about it, but more can be said.
The difference between static and object method call isn&#8217;t about speed at [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve read <a href="http://www.alexatnet.com/node/196">this post</a> about micro-optimization today, and I think many of the listed hints are wrong. Not wrong in way of being false, but in a way that you shouldn&#8217;t use them. Sebastian already <a href="http://sebastian-bergmann.de/archives/854-Do-Not-Micro-Optimize.html">wrote something about it</a>, but more can be said.</p>
<p>The difference between static and object method call isn&#8217;t about speed at all, it&#8217;s about what you&#8217;re dealing with and what you&#8217;re trying to do. If you have an object, you call an object method. If you have a class with a static method, you call the static method. You shouldn&#8217;t choose between the two just for the sake of speed. You shouldn&#8217;t even have the option of choosing. They&#8217;re not interchangeable. And, in most cases you should be using objects, not classes, for testability&#8217;s sake.</p>
<p>A similar case can be made for &#8220;accessing a global variable is faster then an object property&#8221;. If I start developing with this hint in mind, and I start putting object properties in the global scope, what I am going to get? A mess, that&#8217;s what. You should never think about putting an object property in the global scope, for <em>any</em> reason, optimization included.</p>
<p>Another example would be &#8220;an array is a faster alternative to a class with several fields&#8221;. (Should be object, not class.) Again, this makes little sense. An array is a hash-like storage of data, an object is a black box that receives and sends messages, it&#8217;s encapsulated data with behavior attached. These two are two different things, and if I change the code to use arrays instead of objects, the change ripples throughout the rest of the code. Again, this leads to messy code. Not to mention that in the spirit of OOP you <em>should</em> be aiming to use objects, not arrays. Sure, there are cases where this hint would be suitable, but optimization like this should be the last thing you think about.</p>
<p>There are more hints like this, but I&#8217;m not going to list them all, because I&#8217;ve made my point: most of the hints are invalid, because they compare non-interchangeable things. If I push it a little, it&#8217;s almost like saying: not using echo is faster than using it. Yes, it&#8217;s completely true, but these two options <em>don&#8217;t do the same thing</em>.</p>
<p>I can&#8217;t help myself, so I&#8217;ll say a little something about the famous PHP quotes optimization: single quotes are faster than double quotes. First, these two aren&#8217;t the same, so changing one to another ripples out. Second, with quotes it&#8217;s only about readability and taste. Third, the speed gain is literally negligible. This is another hint you should forget as soon as possible. A second spent on quotes optimization is a second lost. I just lost a few minutes writing this, but some readers will hopefully gain them by not bothering about optimizing quotes.</p>
<p>A lot can be said about optimization, but most of these hints sure aren&#8217;t worth remembering. Even more; forget them as soon as possible. If you have a speed problem, find out where it is, and fix it. In the vast majority of the time it&#8217;s IO-related resources: the database and files, maybe network shares, or whatnot. I&#8217;ve <em>never</em> seen a real-life problem where <em>for</em> statement was causing problems and <em>foreach</em> solved them. This just doesn&#8217;t happen, except maybe if you&#8217;re computing Pi in php-cli.</p>
<p>The path to optimization should be:</p>
<ol>
<li>Write working code, no matter how slow it is. It&#8217;s a million times better than fast code with bugs.</li>
<li>If and only if you undoubtedly have performance issues, profile your code, locate and measure slow code.</li>
<li>Optimize the slowest thing. <em>Only</em> the slowest thing.</li>
<li>Loop.</li>
</ol>
<p>Also keep in your mind that by optimizing your code, you reduce readability, and consequently maintainability. This means that you&#8217;ll lose more time the next time you come back to update or fix the code.</p>
<p>There are also some good points that Alex makes, so I&#8217;ll repeat them here, because these <em>are</em> worth remembering:</p>
<ul>
<li>use prepared database statements,</li>
<li>avoid @ (error control operator),</li>
<li>avoid notices and warnings in your scripts.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://kozak.si/widethoughts/2009/03/10/avoid-micro-optimizations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
