<?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>Dominique Stender &#187; static property</title>
	<atom:link href="http://www.st-webdevelopment.com/tag/static-property/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.st-webdevelopment.com</link>
	<description>Good software is only the beginning...</description>
	<lastBuildDate>Wed, 14 Apr 2010 17:50:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Another reason to upgrade to PHP 5.3</title>
		<link>http://www.st-webdevelopment.com/php/2009/12/upgrade-reason-php-53/</link>
		<comments>http://www.st-webdevelopment.com/php/2009/12/upgrade-reason-php-53/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 10:45:33 +0000</pubDate>
		<dc:creator>Dominique</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[object orientation]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[static property]]></category>

		<guid isPermaLink="false">http://www.st-webdevelopment.com/?p=282</guid>
		<description><![CDATA[Dominique Stender outlines why to upgrade to PHP 5.3 by showcasing an issue in the handling of static properties with PHP 5.2]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-287" title="php code" src="http://www.st-webdevelopment.de/wp-content/uploads/2009/12/static.jpg" alt="php code" width="190" height="400" />In case you haven't found any specific reason to switch to PHP 5.3, I have one: Static properties.</p>
<p>Consider the following scenario: You have a group of classes, each responsible for returning a specific statistic. All these classes implement the same API, as given by a common abstract class.</p>
<p>Now, some of these statistics are publicly accessible, others require a certain access control permission to be set.</p>
<p>The logical approach would be to implement a static property to the abstract class, denying access by default. Those classes that are indeed public will overwrite the static property with the correct setting and become accessible.</p>
<p>The property is static to gain speed: There is no need to instantiate the class, if the access control is not satisfied.</p>
<p>Stripped down to the bare essentials the whole set of classes to outline the issue looks like this:</p>
<pre name="code" class="php">abstract class StatisticBase {
    public static $acl = '0';
} // end: class StatisticBase

class PublicStatistic extends StatisticBase {
    public static $acl = '1';
} // end: class StatisticBase

class AdminStatistic extends StatisticBase {
    // default value for $acl is fine
} // end: class AdminStatistic</pre>
<p>AdminStatistic is an implemented class that is private, indicated by the $acl property being 0. PublicStatistic on the other hand is public, hence $acl is 1.</p>
<p>Now before instantiating a specific class we can check whether or not the access condition is satisfied:</p>
<pre name="code" class="php">if ($user-&gt;isAdmin == true || PublicStatistic::$acl == 1) {
    // retrieve admin statistic...
} // end: if</pre>
<p>But naturally this is cumbersome as soon as you have more than a handful of statistics - you don't want to add another condition to your if-statement whenever you add a statistic. So the logical solution is a loop:</p>
<pre name="code" class="php">$statistics = array(
    'PublicStatistic',
    'AdminStatistic'
);

foreach ($statistics as $statClassName) {

    if ($user-&gt;acl == 1 || $statClassName::$acl == 0) {
        // retrieve current statistic...
    } // end: if
} // end: foreach</pre>
<p>Here PHP 5.2 will fail with a fatal error.</p>
<p>You can't access static class properties by means of using a variable for the classname. PHP 5.3 can. There is no really elegant solution for this. (If you know one, post it in the comments!)</p>
<p>While I'm glad that PHP 5.3 has one more OOP feature covered, It leaves me with the feeling that we still have a long way to go until PHP fully supports object orientation.</p>
<p>Now, if Novel would please release a PHP 5.3 package for SLES 10 and SLES 11... thank you</p>
]]></content:encoded>
			<wfw:commentRss>http://www.st-webdevelopment.com/php/2009/12/upgrade-reason-php-53/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
