<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users948</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/948" rel="self"/>
  <title>Gerry</title>
  <updated>Sat Aug 16 16:49:25 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15166</id>
    <published>2008-08-16T16:49:25-07:00</published>
    <title>[PHP] On Random password</title>
    <content type="html">&lt;p&gt;Slight rework of Ted's code.&lt;/p&gt;

&lt;p&gt;Main change is using mt_rand() instead of rand(), which is probably the cause of the speed issues which he mentioned.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
	function randomPassword($length = 6){
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
		$charsLen = strlen($chars)-1;
		$pass = '';
		
		for($n =0; $n &amp;lt; $length; $n++){
			$pass .= substr($chars, mt_rand(0, $charsLen), 1);
		}
		
		return $pass;
	}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Gerry</name>
      <email>gerry.spm+viaMoi@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/90-random-password/refactors/15166" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15130</id>
    <published>2008-08-16T03:25:07-07:00</published>
    <title>[PHP] On Cache any PHP function</title>
    <content type="html">&lt;p&gt;Disagree with comment #1: the conditional is checked regardless in your code, you have just added a redundant assignment.
&lt;br /&gt;Disagree with comment #2: Just makes the code more difficult to read with little gain.&lt;/p&gt;

&lt;p&gt;Code changes:
&lt;br /&gt;- The &amp;quot;//$rebuild= false; // FORCE REBUILD SKIP for WINDOWS&amp;quot; line doesn't make any sense as adding it would just mean that the cache file is never created or updated.
&lt;br /&gt;- Removed need for the problematic @ operator which I personally believe causes more trouble than it's worth in the long run.
&lt;br /&gt;- Changed the two return statements into one so as not to have &amp;quot;Spaghetti Code&amp;quot;.
&lt;br /&gt;- Reduced the chance of accidental conflict arising in the $cacheKey (although still far from perfect)
&lt;br /&gt;- Changed the $timeout from minutes to seconds to reduce code complexity and provide more flexibility.
&lt;br /&gt;- Added PHP tags to the code so that the syntax highlighter would work.&lt;/p&gt;

&lt;p&gt;I still think this code is far from perfect though, especially with possible conflicts of the cache key. There is probably a much better way of creating it.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
	function cache_function($buildCallback, array $args = array(), $timeoutSeconds = 3600){
		// Set up the filename for the cache file 
		if(is_array($buildCallback)){
			$cacheKey = get_class($buildCallback[0]) .'::'. $buildCallback[1];
		}else{
			$cacheKey = $buildCallback . ':' . implode(':', $args);
		}
		$cacheKey .= ':' . implode(':', $args);
		$file_path = CACHE_PATH . md5($cacheKey);
	
		// If the file hasn't yet been created or is out of date then call the require function and store it's result.
		if(!file_exists($file_path) OR filemtime($file_path) &amp;lt; (time() - $timeoutSeconds)){
			$result = call_user_func_array($buildCallback, $args);
			file_put_contents($file_path, serialize($result), LOCK_EX);
		// Else, grab the result from the cache.
		}else{
			$result = unserialize(file_get_contents($file_path));
		}
		
		return $result;
	}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Gerry</name>
      <email>gerry.spm+viaMoi@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/77-cache-any-php-function/refactors/15130" rel="alternate"/>
  </entry>
</feed>

