<?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>The Deployer &#187; PHP</title>
	<atom:link href="http://thedeployer.com/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://thedeployer.com</link>
	<description>Technology and Development Today</description>
	<lastBuildDate>Thu, 05 Aug 2010 07:17:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP on steroids &#8211; HipHop PHP</title>
		<link>http://thedeployer.com/2010-04-php-on-steroids-hiphop-php</link>
		<comments>http://thedeployer.com/2010-04-php-on-steroids-hiphop-php#comments</comments>
		<pubDate>Thu, 08 Apr 2010 18:31:13 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=296</guid>
		<description><![CDATA[HipHop transforms your PHP source code into highly optimized C++ and then compiles it with g++ to build binary files. You keep coding in simpler PHP, then HipHop executes your source code in a semantically equivalent manner and sacrifices some rarely used features &#8211; such as eval() &#8211; in exchange for improved performance.
More info here:

News: [...]]]></description>
			<content:encoded><![CDATA[<p>HipHop transforms your PHP source code into highly optimized C++ and then compiles it with g++ to build binary files. You keep coding in simpler PHP, then HipHop executes your source code in a semantically equivalent manner and sacrifices some rarely used features &#8211; such as eval() &#8211; in exchange for improved performance.</p>
<p>More info here:</p>
<ul>
<li><a href="http://developers.facebook.com/news.php?story=358&#038;blog=1">News: HipHop for PHP: Move Fast</a></li>
<li><a href="http://developers.facebook.com/hiphop-php/">Facebook Open-Sources HipHop PHP Compiler Software</a></li>
<li><a href="http://wiki.github.com/facebook/hiphop-php/">git &#8211; Facebook / HipHop-PHP</a></li>
<li><a href="http://groups.google.com/group/hiphop-php-dev?pli=1">Google Groups &#8211; HipHop for PHP Dev</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2010-04-php-on-steroids-hiphop-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why You Can’t Work at Work</title>
		<link>http://thedeployer.com/2010-03-why-you-can%e2%80%99t-work-at-work</link>
		<comments>http://thedeployer.com/2010-03-why-you-can%e2%80%99t-work-at-work#comments</comments>
		<pubDate>Tue, 30 Mar 2010 10:19:33 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rapid development]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=291</guid>
		<description><![CDATA[Lovely presentation, except the fact that it is advertising the products of 37signals, it does hold some truth:

If the embed doesn&#8217;t work, check it out here: http://bigthink.com/ideas/18522
]]></description>
			<content:encoded><![CDATA[<p>Lovely presentation, except the fact that it is advertising the products of 37signals, it does hold some truth:</p>
<p><script src="http://video.bigthink.com/player.js?deepLinkEmbedCode=03NG42MTqVnn6kOnuDv8k_iDC2HEGniT&#038;embedCode=03NG42MTqVnn6kOnuDv8k_iDC2HEGniT&#038;height=288&#038;autoplay=0&#038;width=512"></script></p>
<p>If the embed doesn&#8217;t work, check it out here: <a href="http://bigthink.com/ideas/18522">http://bigthink.com/ideas/18522</a></p>
]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2010-03-why-you-can%e2%80%99t-work-at-work/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kohana &amp; FastCGI routing problems</title>
		<link>http://thedeployer.com/2009-10-kohana-fastcgi-routing-problems</link>
		<comments>http://thedeployer.com/2009-10-kohana-fastcgi-routing-problems#comments</comments>
		<pubDate>Sat, 03 Oct 2009 18:11:54 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[kohana php router]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=289</guid>
		<description><![CDATA[If you&#8217;re developing on Kohana framework and your server is setup with Apache and FastCGI, you&#8217;ll run into some weird trouble regarding URL processing. This is happening because Kohana&#8217;s Router class is not looking at the REQUEST_URI key in the $_SERVER global, and gets the PHP_SELF instead that always returns the index.php file.
This has a [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re developing on Kohana framework and your server is setup with Apache and FastCGI, you&#8217;ll run into some weird trouble regarding URL processing. This is happening because Kohana&#8217;s Router class is not looking at the REQUEST_URI key in the $_SERVER global, and gets the PHP_SELF instead that always returns the index.php file.</p>
<p>This has a workaround: either you alter the find_uri() method in the Router class, or you change the way the parameters are sent to the script via .htaccess&#8217;s mod_rewrite rules, as described in <a href="http://projects.kohanaphp.com/issues/1923">http://projects.kohanaphp.com/issues/1923</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2009-10-kohana-fastcgi-routing-problems/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Listing files in a directory with PHP</title>
		<link>http://thedeployer.com/2009-05-listing-files-in-a-directory-with-php</link>
		<comments>http://thedeployer.com/2009-05-listing-files-in-a-directory-with-php#comments</comments>
		<pubDate>Mon, 18 May 2009 08:27:57 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[directory]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=277</guid>
		<description><![CDATA[Listing the files in a directory in PHP is as simple as 1 line:

$aList = scandir&#40; '/tmp' &#41;;
print_r&#40; $aList &#41;;

This will retrieve all the files in the specified directory (including &#8220;.&#8221; and &#8220;..&#8221;):

Array
&#40;
    &#91;0&#93; =&#62; .
    &#91;1&#93; =&#62; ..
    &#91;2&#93; =&#62; uioeng34890gn34.dat
&#41;

]]></description>
			<content:encoded><![CDATA[<p>Listing the files in a directory in PHP is as simple as 1 line:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$aList</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'/tmp'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$aList</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will retrieve all the files in the specified directory (including &#8220;.&#8221; and &#8220;..&#8221;):</p>

<div class="wp_syntax"><div class="code"><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> <span style="color: #339933;">.</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> <span style="color: #339933;">..</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> uioeng34890gn34<span style="color: #339933;">.</span>dat
<span style="color: #009900;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2009-05-listing-files-in-a-directory-with-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why should we use PHP constants inside classes</title>
		<link>http://thedeployer.com/2009-04-why-should-we-use-php-constants-inside-classes</link>
		<comments>http://thedeployer.com/2009-04-why-should-we-use-php-constants-inside-classes#comments</comments>
		<pubDate>Tue, 14 Apr 2009 20:58:19 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[Featured Articles]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[constants]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=237</guid>
		<description><![CDATA[constant
A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren&#8217;t actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

Practically, it&#8217;s as easy as:

define&#40;'DATABASE', 'thedatabase'&#41;;

Constants are useful in a lot [...]]]></description>
			<content:encoded><![CDATA[<dl>
<dt><strong>constant</strong></dt>
<dd>A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for <a href="http://www.php.net/manual/en/language.constants.predefined.php">magic constants</a>, which aren&#8217;t actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.</dd>
</dl>
<p>Practically, it&#8217;s as easy as:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DATABASE'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thedatabase'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Constants are useful in a lot of cases:
<ul>
<li><b>Configuration directives</b>
<p>Storing database connection information is plain easy and available everywhere.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'DATABASE'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thedatabase'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'USERNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'theuser'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'PASSWORD'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thepass'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'HOSTNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'localhost'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</p>
<p>These have global scope, cannot be tempered with and can be accessed from everywhere. How easy is that?</p>
</li>
<li><b>Anti-hack techniques</b>
<p>You can make sure nobody accesses and uses your files remotely by checking a constant that you only define in one place, unaccessible from the web.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ANTIHACK_FG3N4890FN4334G3GH'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You naughty'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li><b>Various flags</b>
<p>Usually you need here and there to set some flags that activate or dezactivate some functionalities in your application, including debug flags.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'USE_CACHE'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//.... some code....</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">constant</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'USE_CACHE'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//... cache data here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>By using
<pre>constant()</pre>
<p>, we don&#8217;t get a notice if the constant is not defined, and to disable the cache, you can take any approach you find suited: either set the defined constant to &#8220;false&#8221;, or comment it, the outcome is the same and you don&#8217;t get notices and warnings in your code, even if you completely remove the constant from your code.</p>
</li>
</ul>
<p>So, you define the constants with <b>define()</b>, you check to see if certain constant is defined with the <b>defined()</b> function and you get the value of a constant that could or could not be defined using <b>constant()</b>.</p>
<p><strong>Using constants in classes</strong></p>
<p>Along with the wonders of OOP in PHP 5 came the possibility to define constants inside classes. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AppSettings <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">const</span> DATABASE <span style="color: #339933;">=</span> <span style="color: #0000ff;">'thedeployer'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">const</span> HOSTNAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">const</span> USERNAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">'deployer'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">const</span> PASSWORD <span style="color: #339933;">=</span> <span style="color: #0000ff;">'thepassword'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'database = `'</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">DATABASE</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'`'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'hostname = `'</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">HOSTNAME</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'`'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'username = `'</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">USERNAME</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'`'</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'password = `'</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">PASSWORD</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'`'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// END constructor</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// END class</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'database from outside = '</span> <span style="color: #339933;">.</span> AppSettings<span style="color: #339933;">::</span><span style="color: #004000;">DATABASE</span><span style="color: #339933;">;</span></pre></div></div>

<p>Basically, the constants ar a part of the class definition, you don&#8217;t have to actually execute code to define the constants and they are available immediately for use. And most important, you don&#8217;t have to instantiate a class to access it&#8217;s constants, and you always know where to look for the definition.</p>
<p>The advantages and differences that one should remember when using constants and the reasons to use constants in classes rather than just floating around in your code are:</p>
<ul>
<li><strong>Constants defined in classes do not show up in the global scope.</strong>
<p>There is a stack where PHP holds all the constants and it&#8217;s a rather large list. It&#8217;s nothing like the variables which are a total of 4 superglobals that exists when a script starts. To see the list of defined constants, just run:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">get_defined_constants</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You&#8217;ll get a huge list of approximately 1760 defined constants when your script hasn&#8217;t got a line. This is a required compromise, because these constants include TRUE, FALSE, error codes, constants defined by enabled extensions and some other useful constants. This is why your constants shouldn&#8217;t go here. If you define your constants as part of some classes, they remain within the class definition and don&#8217;t end up in the constants pool. And don&#8217;t worry, even if they&#8217;re defined within a class, they are available everywhere, just like a normal constant.</p>
</li>
<li><strong>Grouping the same type of data together.</strong>
<p>Usually, you can define a class that just deals with every piece of configuration data your application needs, just like de example above. This means that all your constants are in one place, and if you need to change a constant, you only need to find where the class is defined and change your info. You even have the class name, because you&#8217;ll be using the constants defined in some class this way:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'database from outside = '</span> <span style="color: #339933;">.</span> AppSettings<span style="color: #339933;">::</span><span style="color: #004000;">DATABASE</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// this means that the &quot;DATABASE&quot; constant is defined in the &quot;AppSettings&quot; class.</span></pre></div></div>

<p>How easy is that?</p>
</li>
<li><strong>Available without executing code.</strong>
<p>Since the constants are part of the class definition, they&#8217;re available before even the first line of actual code is executed.</p>
</li>
<li><strong>Class autoloading power.</strong>If you have the autoload feature in use in your application, you&#8217;ll get another advantage. You&#8217;ll have the constants defined with a single call to the wanted class that contain the constants. The autoloader will locate an load you class.</li>
</ul>
<p>These are just a few things that need to be said and noted when addressing constants. Be careful not to use constants where they&#8217;re not appropriate. For example in internationalization. Don&#8217;t define the i18n labels in constants. That&#8217;s a very greedy approach. Use <a href="http://www.php.net/gettext">gettext</a> extension or some other dedicated solution for this. Using constants is just bad.</p>
<p>Happy coding and have a nice day!</p>
]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2009-04-why-should-we-use-php-constants-inside-classes/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>APC &#8211; Alternative PHP Cache</title>
		<link>http://thedeployer.com/2009-03-apc-alternative-php-cache</link>
		<comments>http://thedeployer.com/2009-03-apc-alternative-php-cache#comments</comments>
		<pubDate>Sun, 29 Mar 2009 21:53:17 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[Featured Articles]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[APC]]></category>
		<category><![CDATA[cache]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=142</guid>
		<description><![CDATA[APC is a simple and yet powerful PHP extension that does just that: caching. What for? Suppose you would have some piece of code that fetches some data from a table, and since you don&#8217;t use JOIN because you loose scalability, you&#8217;ll have to do some more SELECTs, and then format each row to be [...]]]></description>
			<content:encoded><![CDATA[<p>APC is a simple and yet powerful PHP extension that does just that: caching. What for? Suppose you would have some piece of code that fetches some data from a table, and since you don&#8217;t use JOIN because you loose scalability, you&#8217;ll have to do some more SELECTs, and then format each row to be ready for display.</p>
<p>The other advantage is that it caches the compiled PHP source code in memory, serving it <strong>a lot</strong> faster.</p>
<p>Now if 100 users to this in one minute, (1) your server will fail to serve the pages because (2) it is doing a very complicated task that outputs the same result throughout the minute. This is where APC comes handy. You can save the retrieved data in the APC cache which holds it in memory and then just serve it to whoever asks for it.</p>
<p>The first thing to do is check if the APC extension is installed and running. For that you can look at phpinfo()&#8217;s result and check for APC. If it&#8217;s not, you should look in the php.ini file for the extension directive to be uncommented.</p>
<p>Using the APC cache requires three things:</p>
<ul>
<li>a name (key) for the piece of information saved;</li>
<li>the actual data;</li>
<li>the TTL (time to live): how many seconds the information will be held in the cache.</li>
</ul>
<p>The syntax to add data in the cache is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">bool apc_add <span style="color: #009900;">&#40;</span> string <span style="color: #000088;">$key</span><span style="color: #339933;">,</span> mixed <span style="color: #000088;">$var</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> int <span style="color: #000088;">$ttl</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p>So, $key is the name under which we&#8217;ll find the saved info, $var is the actual data and $ttl is an optional parameter to specify a time to live. If this function returns FALSE, something went wrong and the information isn&#8217;t cached. You&#8217;ll have to have some sort of fallback and never rely on the fact that you&#8217;re info is safe in the cache.</p>
<p>To retrieve the cached data, use apc_fetch():</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">mixed apc_fetch <span style="color: #009900;">&#40;</span> string <span style="color: #000088;">$key</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> bool <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$success</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p>It&#8217;s that easy. Just ask for the saved key and you get it. If it&#8217;s not found or something goes wrong, this function returns FALSE and if the optional parameter was specified, sets that one too to FALSE.</p>
<p>That&#8217;s it. You have an up and running cache system.</p>
<p><strong>Good practice</strong>: Regarding the usage of cache, there are two things you should keep in mind:</p>
<ol>
<li>you should build your code like there can be no caching done; that means 100% fallback. If the APC extension is disabled, your code should work.</li>
<li>the specific APC caching functions should be wrapped up in some generic functions that call the actual functions. This thing will make you go light up a candle when &#8211; at some point &#8211; you&#8217;ll be switching to memcached or some other kind of caching, because you&#8217;ll only have to modify the contents of these wrapper functions, and not search &amp; replace the entries in all your code.</li>
</ol>
<p>Here&#8217;s an example on how to acomplish both these things:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> cacheWrite<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sKey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mData</span><span style="color: #339933;">,</span> <span style="color: #000088;">$iTtl</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'apc_add '</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// we have APC</span>
    <span style="color: #b1b100;">return</span> apc_add<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sKey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mData</span><span style="color: #339933;">,</span> <span style="color: #000088;">$iTtl</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// END func cacheWrite()</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> cacheRead<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sKey</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'apc_fetch'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// we have APC</span>
    <span style="color: #b1b100;">return</span> apc_fetch<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sKey</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// END func cacheRead()</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//---------- code that read/writes</span>
&nbsp;
<span style="color: #000088;">$sKey</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sQuery</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// same query will return same data, whithin minutes</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$mData</span> <span style="color: #339933;">=</span> cacheRead<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sKey</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// info not cached</span>
  <span style="color: #000088;">$mData</span> <span style="color: #339933;">=</span> functionThatMakesUpTheData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  cacheWrite<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sKey</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mData</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">600</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// cache the data for 10 minutes</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// done, here we have our data, no matter if caching functions are available</span>
<span style="color: #666666; font-style: italic;">// or not, if the info is in the cache or not</span></pre></div></div>

<p>And to make it even more fun, APC comes with a nice online page that gives you all the information you need, including cache hits &amp; misses (misses happen when the cache memory is full), a list of all the user-space cached data along with options to view or delete one or all the entries and much more.</p>
]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2009-03-apc-alternative-php-cache/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to setup Eclipse + XAMPP + Zend Optimizer + Debugging</title>
		<link>http://thedeployer.com/2008-10-how-to-setup-eclipse-xampp-zend-optimizer-debugging</link>
		<comments>http://thedeployer.com/2008-10-how-to-setup-eclipse-xampp-zend-optimizer-debugging#comments</comments>
		<pubDate>Sat, 11 Oct 2008 15:02:00 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[IDE]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=55</guid>
		<description><![CDATA[[ This is currently a draft. will format and finish later ]
Eclipse all-in-one here:
Zend debugger here: http://downloads.zend.com/pdt/server-debugger/
How to allow Zend Debugger and Zend Optimizer to co-exist: http://blog.tigeryao.com/2008/how-to-allow-zend-optimizer-and-zend-debugger-coexist.html
XAMPP download:
Instructions on how to enable debugging for Eclipse:
Zend Debugger installation instructions
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;
1. Locate ZendDebugger.so or ZendDebugger.dll file that is compiled for the
correct version of PHP (4.3.x, 4.4.x, 5.0.x, 5.1.x, [...]]]></description>
			<content:encoded><![CDATA[<p>[ This is currently a draft. will format and finish later ]</p>
<p>Eclipse all-in-one here:<br />
Zend debugger here: http://downloads.zend.com/pdt/server-debugger/<br />
How to allow Zend Debugger and Zend Optimizer to co-exist: http://blog.tigeryao.com/2008/how-to-allow-zend-optimizer-and-zend-debugger-coexist.html<br />
XAMPP download:</p>
<p>Instructions on how to enable debugging for Eclipse:</p>
<blockquote><p>Zend Debugger installation instructions<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>1. Locate ZendDebugger.so or ZendDebugger.dll file that is compiled for the<br />
correct version of PHP (4.3.x, 4.4.x, 5.0.x, 5.1.x, 5.2.x) in the<br />
appropriate directory.</p>
<p>2. Add the following line to the php.ini file:<br />
Linux and Mac OS X:     zend_extension=/full/path/to/ZendDebugger.so<br />
Windows:                zend_extension_ts=/full/path/to/ZendDebugger.dll<br />
Windows non-tread safe: zend_extension=/full/path/to/ZendDebugger.dll</p>
<p>(*) the windows non-thread safe is used only with Zend Core 2.0</p>
<p>3. Add the following lines to the php.ini file:<br />
zend_debugger.allow_hosts=<br />
zend_debugger.expose_remotely=always</p>
<p>4. Place dummy.php file in the document root directory.</p>
<p>5. Restart web server.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2008-10-how-to-setup-eclipse-xampp-zend-optimizer-debugging/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML2Array</title>
		<link>http://thedeployer.com/2008-10-xml2array</link>
		<comments>http://thedeployer.com/2008-10-xml2array#comments</comments>
		<pubDate>Sat, 04 Oct 2008 15:06:00 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=54</guid>
		<description><![CDATA[I&#8217;ve seen here the shortest xml2array implementation:
function xml2array($xml) {
$xmlary = array();
$reels = '/&#60;(\w+)\s*([^\/&#62;]*)\s*(?:\/&#62;&#124;&#62;(.*)&#60;\/\s*\\1\s*&#62;)/s';
$reattrs = '/(\w+)=(?:"&#124;\')([^"\']*)(:?"&#124;\')/';
preg_match_all($reels, $xml, $elements);
foreach ($elements[1] as $ie =&#62; $xx) {
$xmlary[$ie]["name"] = $elements[1][$ie];
if ($attributes = trim($elements[2][$ie])) {
preg_match_all($reattrs, $attributes, $att);
foreach ($att[1] as $ia =&#62; $xx)
$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
}
$cdend = strpos($elements[3][$ie], "&#60;");
if ($cdend &#62; 0) {
$xmlary[$ie]["text"] = substr($elements[3][$ie], 0, $cdend - 1);
}
if (preg_match($reels, $elements[3][$ie]))
$xmlary[$ie]["elements"] = xml2array($elements[3][$ie]);
else [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen <a href="http://www.bytemycode.com/snippets/snippet/445/">here</a> the shortest xml2array implementation:</p>
<p><code>function xml2array($xml) {<br />
$xmlary = array();</p>
<p>$reels = '/&lt;(\w+)\s*([^\/&gt;]*)\s*(?:\/&gt;|&gt;(.*)&lt;\/\s*\\1\s*&gt;)/s';<br />
$reattrs = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';</p>
<p>preg_match_all($reels, $xml, $elements);</p>
<p>foreach ($elements[1] as $ie =&gt; $xx) {<br />
$xmlary[$ie]["name"] = $elements[1][$ie];</p>
<p>if ($attributes = trim($elements[2][$ie])) {<br />
preg_match_all($reattrs, $attributes, $att);<br />
foreach ($att[1] as $ia =&gt; $xx)<br />
$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];<br />
}</p>
<p>$cdend = strpos($elements[3][$ie], "&lt;");<br />
if ($cdend &gt; 0) {<br />
$xmlary[$ie]["text"] = substr($elements[3][$ie], 0, $cdend - 1);<br />
}</p>
<p>if (preg_match($reels, $elements[3][$ie]))<br />
$xmlary[$ie]["elements"] = xml2array($elements[3][$ie]);<br />
else if ($elements[3][$ie]) {<br />
$xmlary[$ie]["text"] = $elements[3][$ie];<br />
}<br />
}</p>
<p>return $xmlary;<br />
}</code></p>
]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2008-10-xml2array/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP file operations on large files fail</title>
		<link>http://thedeployer.com/2008-09-php-file-operations-on-large-files-fail</link>
		<comments>http://thedeployer.com/2008-09-php-file-operations-on-large-files-fail#comments</comments>
		<pubDate>Thu, 11 Sep 2008 09:36:00 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=52</guid>
		<description><![CDATA[If you have a 2.7 GB file, file_exists(&#8216;./filename&#8217;) works but is_file( &#8216;./filename&#8217; ); fails gracefully. Not even Apache handles these kind of files very well so take care.
]]></description>
			<content:encoded><![CDATA[<p>If you have a 2.7 GB file, <span style="font-weight:bold;">file_exists(&#8216;./filename&#8217;)</span> works but <span style="font-weight:bold;">is_file( &#8216;./filename&#8217; );</span> fails gracefully. Not even Apache handles these kind of files very well so take care.</p>
]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2008-09-php-file-operations-on-large-files-fail/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Cron Daemon</title>
		<link>http://thedeployer.com/2007-12-php-cron-daemon</link>
		<comments>http://thedeployer.com/2007-12-php-cron-daemon#comments</comments>
		<pubDate>Tue, 04 Dec 2007 16:08:00 +0000</pubDate>
		<dc:creator>Lucian Daniliuc</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://thedeployer.com/?p=35</guid>
		<description><![CDATA[

Name:
PHP Cron Daemon


Base name:
phpcrondaemon


Description:
Database driven PHP job scheduler like cron


Search tags:
cron, schedule,  daemon, scheduler


Version:
0.1


Required PHP version:
4.0


License:
GNU General Public License (GPL)



Adresa:
http://www.phpclasses.org/browse/package/4140.html
]]></description>
			<content:encoded><![CDATA[<table height="280" width="100%">
<tbody>
<tr bgcolor="#dcdce0">
<th align="right" nowrap="nowrap" valign="top" width="1%">Name:</th>
<td><b><span class="nfakPe">PHP</span> <span class="nfakPe">Cron</span> Daemon</b><a href="http://www.phpclasses.org/discuss/package/4140/" target="_blank"></a></td>
</tr>
<tr bgcolor="#d0d0d4">
<th align="right" nowrap="nowrap" width="1%">Base name:</th>
<td><tt><a href="http://www.phpclasses.org/phpcrondaemon" target="_blank">phpcrondaemon</a></tt></td>
</tr>
<tr bgcolor="#dcdce0">
<th align="right" nowrap="nowrap" width="1%">Description:</th>
<td><b>Database driven <span class="nfakPe">PHP</span> job scheduler like <span class="nfakPe">cron</span></b></td>
</tr>
<tr bgcolor="#d0d0d4">
<th align="right" nowrap="nowrap" width="1%">Search tags:</th>
<td nowrap="nowrap"><a href="http://www.phpclasses.org/searchtag/cron/by/package.html" target="_blank"><b><span class="nfakPe">cron</span></b></a>, <a href="http://www.phpclasses.org/searchtag/schedule/by/package.html" target="_blank"><b>schedule</b></a>, <a href="http://www.phpclasses.org/searchtag/daemon/by/package.html" target="_blank"> <b>daemon</b></a>, <a href="http://www.phpclasses.org/searchtag/scheduler/by/package.html" target="_blank"><b>scheduler</b></a></td>
</tr>
<tr bgcolor="#dcdce0">
<th align="right" nowrap="nowrap" width="1%">Version:</th>
<td><tt>0.1</tt></td>
</tr>
<tr bgcolor="#d0d0d4">
<th align="right" nowrap="nowrap" width="1%">Required <span class="nfakPe">PHP</span> version:</th>
<td><tt>4.0</tt></td>
</tr>
<tr bgcolor="#dcdce0">
<th align="right" nowrap="nowrap" valign="top" width="1%">License:</th>
<td><a href="http://www.opensource.org/licenses/gpl-license.html" target="_blank">GNU General Public License (GPL)</a></td>
</tr>
</tbody>
</table>
<p>Adresa:</p>
<p><a href="http://www.phpclasses.org/browse/package/4140.html" target="_blank">http://www.phpclasses.org<wbr>/browse/package/4140.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://thedeployer.com/2007-12-php-cron-daemon/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
