<?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>PHP vs .Net</title>
	<atom:link href="http://www.phpvs.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpvs.net</link>
	<description>ASP.Net and PHP go head to head</description>
	<lastBuildDate>Thu, 01 Jul 2010 05:52:22 +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>Manually validate an ASP.Net MVC form on the client side with MicrosoftMvcValidation.js and jQuery</title>
		<link>http://www.phpvs.net/2010/04/26/manually-validate-an-asp-net-mvc-form-on-the-client-side-with-microsoftmvcvalidation-js-and-jquery/</link>
		<comments>http://www.phpvs.net/2010/04/26/manually-validate-an-asp-net-mvc-form-on-the-client-side-with-microsoftmvcvalidation-js-and-jquery/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 06:16:51 +0000</pubDate>
		<dc:creator>morgan</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[asp.net asp.net-mvc jquery validation]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/?p=196</guid>
		<description><![CDATA[A recent problem cropped up in my Asp.Net MVC application.  Its using the standard setup of DataAnnotations + MicrosoftMvcValidation.js + jQuery 1.4.2, and I needed to check the validation state of a form before performing some client-side actions. No problem, right?
Obviously not.
This second part of this post gets in depth as to what&#039;s going [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-213" title="check-x" src="http://www.phpvs.net/wp-content/uploads/2010/04/check-x.png" alt="Validation" width="171" height="165" />A recent problem cropped up in my Asp.Net MVC application.  Its using the standard setup of DataAnnotations + MicrosoftMvcValidation.js + jQuery 1.4.2, and I needed to check the validation state of a form before performing some client-side actions. No problem, right?</p>
<p>Obviously not.</p>
<p>This second part of this post gets in depth as to what&#039;s going on with the MicrosoftMvcValidation script, but to save you some time, I&#039;ll post the solution first.</p>
<p><span style="text-decoration: underline;"><strong>To manually validate your form with MicrosoftMvcValidation.js:</strong></span></p>
<p><del datetime="2010-07-01T05:45:10+00:00">In MicrosoftMvcValidation.js, change line 20 from:</p>
<p style="padding-left: 30px;"><code> return Sys.Mvc._ValidationUtil.$0($2.validate('submit'));})); return $2;</code></p>
<p>to</p>
<p style="padding-left: 30px;"><code> return Sys.Mvc._ValidationUtil.$0($2.validate('submit'));})); <strong>$0.MvcValidationFormContext = $2;</strong> return $2;</code></p>
<p>Alternately, if you&#039;re using the debug version of the script, you can achieve the same effect by adding the following to line 196 (the end of the _parseJsonOptions function):</p>
<p style="padding-left: 30px;"><code>formElement.MvcValidationFormContext = formContext;</code></p>
<p></del></p>
<p><strong>UPDATE: </strong> As Morten Christiansen points out in the comments, there is a much cleaner way of accessing the FormContext object, without having to introduce a custom tracking property and modifying the script.  Instead of a custom property, we can just use<br />
<code>$('form')[0]['__MVC_FormValidation']</code>.  Thanks to Morten for pointing this out!</p>
<p>Now we can use the following jQuery/javascript code to manually validate our form:</p>
<pre>
var $form = ("#MyForm");                            // Select our form with jQuery
<del datetime="2010-07-01T05:45:10+00:00">var context = $form[0].MvcValidationFormContext;    // Access the new property we created</del>
var errors;
if ($form[0]['__MVC_FormValidation']) {
    errors = $form[0]['__MVC_FormValidation'].validate("submit");        // Validate the form
}
if (!$form[0]['__MVC_FormValidation'] || errors.length == 0) {
    // No errors, do your stuff
}
</pre>
<hr />
Now for the explanation.</p>
<p>I assumed (incorrectly) that there must be a client-side API to <code>MicrosoftMvcValidation.js</code>, the javascript responsible for dynamic client side form validation that ships with the ASP.Net MVC framework.  Unfortunately, in all my digging, I could not unearth one -- and it seems like a huge oversight.  The amount of effort to expose an <code>isValid</code> property or a <code>validate()</code> function on the client would be pretty minimal!</p>
<p>Why would you need to do this?  Doesn&#039;t the validation occur automatically?   Well, yes it does -- when you submit the form, or click around in it.  However, there are lots of cases where you might want to manually trigger the error messages or perform a client-side action based on whether or not the form is valid, without running the submit handler.   In my case, I wanted to make sure the form was valid <em>before </em>trying to submit it, as our ajax pipeline puts up the &#034;spinner&#034; graphic when a submit event is triggered, which meant it was flashing briefly and looked dumb.</p>
<p>The most common response I ran across when searching for how to manually validate was -- <a href="http://stackoverflow.com/questions/2060554/asp-net-mvc-check-form-input-is-valid-on-submit">give up and use the jQuery validate plugin</a>.  This would be my preferred solution to be honest, but I ran into at least two blockers with jQuery validate:</p>
<ol>
<li> it doesn&#039;t seem to work with modal dialogs generated from jQuery UI,</li>
<li> it doesn&#039;t seem to work with forms that are loaded into the document via an ajax call.</li>
</ol>
<p>So that was out.  Since I already had most of what I needed working with the Microsoft scripts, I went back to them and started exploring the code to see if I could discover any sort of public API.  This is what I came up with.</p>
<p>The key thing that I found was that <code>MicrosoftMvcValidation</code> creates a javascript object called a <code>FormContext</code>, which has a <code>validate(eventName)</code> method.  This <em>seemed </em>promising, so I tried following the code to see how I could get at this validate method. Well -- I couldn&#039;t.  Not without some modifications.  Here&#039;s how it works.</p>
<p>When you include <code>MicrosoftMvcValidation</code>, the main piece of script that executes is this:</p>
<pre>Sys.Mvc.FormContext._Application_Load = function Sys_Mvc_FormContext$_Application_Load() {
    var allFormOptions = window.mvcClientValidationMetadata;
    if (allFormOptions) {
        while (allFormOptions.length &gt; 0) {
            var thisFormOptions = allFormOptions.pop();
            Sys.Mvc.FormContext._parseJsonOptions(thisFormOptions);
        }
    }
}
</pre>
<p>Adding <code>&lt;% Html.EnableClientValidation(); %&gt;</code> to your views causes a bunch of JSON data to be output to the page, and appended to the <code>window.mvcClientValidationMetatdata</code> javascript object.  When the page is ready, the above function runs and calls <code>FormContext._parseJsonOptions()</code> on all the rules and data that were output to that object.</p>
<p><code>_parseJsonOptions </code>is what creates the <code>FormContext </code>object that we&#039;re interested in.  In fact, the return value of <code>_parseJsonOptions </code><em>is the FormContext</em>&#8230; but sadly, Application_Load ignores the return value, and it&#039;s just thrown to the bitbucket, never to be seen again (at least by us).</p>
<p>Judging from the underscores on the function names, it&#039;s pretty clear that these are meant to be internal operations, so we can (perhaps) excuse them for not storing and exposing the <code>FormContext </code>for us. From the framework&#039;s point of view, it doesn&#039;t need to be stored anywhere, because it still has it -- <code> _parseJsonOptions</code> internally adds new event handlers for the form, and these handler delegates close over the <code>FormContext </code>object.  The closures means that internally, the form handlers still have a saved reference to what they need, so the application load function doesn&#039;t need to do anything with the return value.  The <code>FormContext </code>never surfaces -- so if we want to validate the form ourselves, we need to <del datetime="2010-07-01T05:47:53+00:00">expose</del> dig for it.</p>
<p><del datetime="2010-07-01T05:47:53+00:00">All that my code at the top of the post is doing is modifying the <code>_parseJsonOptions</code> function (minified to be named <code>$12</code>), to start adding a new property to the form element in the DOM, which will store the created <code>FormContext</code> object. I named this property <code>MvcValidationFormContext</code>, but you could call it anything you&#039;d like.  You can subsequently access this new property to get the <code>FormContext </code>and call <code>validate("submit")</code> on it, which fires the validation as though we were trying to submit the form, and returns a collection of errors we can check.</del></p>
<p>As Morten pointed out, the reference is, of course, still there.  You can access the closed over FormContext by grabbing the DOM element and digging in it&#039;s properties array.  The updated code reflects this.</p>
<hr />I&#039;d love to see an upgrade to <code>MicrosoftMvcValidation.js</code> to include a client API in the future.  Even more, I&#039;d love to see <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Validate</a> get some attention to fix the blockers that I ran into, so I can dump the MicrosoftAjax scripts out of my project altogether.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2010/04/26/manually-validate-an-asp-net-mvc-form-on-the-client-side-with-microsoftmvcvalidation-js-and-jquery/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>An Exercise in Wordpress Integration, or Why Wordpress Sucks</title>
		<link>http://www.phpvs.net/2009/12/08/an-exercise-in-wordpress-integration-or-why-wordpress-sucks/</link>
		<comments>http://www.phpvs.net/2009/12/08/an-exercise-in-wordpress-integration-or-why-wordpress-sucks/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 08:23:50 +0000</pubDate>
		<dc:creator>blake</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/?p=154</guid>
		<description><![CDATA[I'd like to prefix my upcoming rant with the fact that Wordpress is good at what it does:  making basic blogs and publishing content.  I use it, many other people use it, it works.  Heck, I'm using it right now.  But from a technical standpoint, Wordpress sucks.  I'm going to [...]]]></description>
			<content:encoded><![CDATA[<p>I'd like to prefix my upcoming rant with the fact that Wordpress is good at what it does:  making basic blogs and publishing content.  I use it, many other people use it, it works.  Heck, I'm using it right now.  But from a technical standpoint, Wordpress sucks.  I'm going to relate my experience here trying write a quick function to store post output to a file, to be used by a separate application on the same server.</p>
<p>I started off to write a function (let's call it a caching function for simplicity) that stores some HTML from the most recently published post.  Sounds easy enough.  I should be able to just put a function into the functions.php file of the custom template set I'm using.  That's seems to be where the "userland" custom functions go.</p>
<p>So I check the <a href="http://codex.wordpress.org/Function_Reference/" target="_blank">function reference</a> first.  Hey, <a href="http://codex.wordpress.org/Function_Reference/wp_get_recent_posts" target="_blank">wp_get_recent_posts()</a>.  Looks promising, so I give it a shot.  It goes ahead and gets the most recent post just fine.  Things are ok so far.</p>
<h2>A problem appears</h2>
<h2><img class="alignleft size-medium wp-image-160" style="margin-left: 10px; margin-right: 10px;" title="storm-at-sea" src="http://www.phpvs.net/wp-content/uploads/2009/12/storm-at-sea-300x224.jpg" alt="storm-at-sea" width="245" height="183" /></h2>
<p>Now, I want to output the post exactly as it would appear in the blog, and save that output to a file on disk.  Surely there's a basic function that will output a post's content?  You know... take the post_content field from the database record and format it properly?  Suddenly, the skies darken.  Evil laughter booms out.  Ha ha ha!  Wordpress mocks the folly of simplistic functional thinking!</p>
<p>The template files use functions like <code>the_content()</code> and <code>the_title()</code>.  Just in case you can't tell from the excellent naming scheme, these actually produce echoed output.  Checking out <code>the_content()</code>, we see it dutifully calls <code>get_the_content()</code>, then runs a couple of lines of formatting stuff on the results.   So how about using <code>get_the_content()</code> for my caching function?  I could run the other few formatting bits manually after that.  Should be ok, right?  After all, the doc comment for <code>get_the_content()</code> says the following:</p>
<p><code>/**<br />
* Retrieve the post content.<br />
*</code></p>
<p>So, I can go ahead assume it simply retrieves the basic post content then?  Ha ha.  <strong>NO</strong>.  <em>WHY WOULD IT DO THAT</em>?  Instead, it takes a bunch of globals that get set who-the-hell-knows-where, runs through a bunch of crap seemingly unrelated to the content of a post, and does a whole lot of textual modifications to <strong>some kind of content</strong>.  Reading through the function is like jabbing red-hot fire pokeys into your eyes.  Here's a portion of it:</p>
<p><code>
<div class="igBar"><span id="lphp-3"><a href="#" onclick="javascript:showPlainTxt('php-3'); return false;">&gt;&gt; show as plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-3">
<div>
<ol>
<li>
<div><span style="color:#0000FF;">$content</span> = <span style="color:#0000FF;">$pages</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF;">$page</span>-<span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li>
<div><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/preg_match"><span style="color:#000066;">preg_match</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'/&amp;lt;<span style="color:#000099; font-weight:bold;">\!</span>--more(.*?)?--&amp;gt;/'</span>, <span style="color:#0000FF;">$content</span>, <span style="color:#0000FF;">$matches</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li>
<div><span style="color:#0000FF;">$content</span> = <a href="http://www.php.net/explode"><span style="color:#000066;">explode</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$matches</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#0000FF;">$content</span>, <span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li>
<div><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span> !<a href="http://www.php.net/empty"><span style="color:#000066;">empty</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$matches</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> &amp;amp;&amp;amp; !<a href="http://www.php.net/empty"><span style="color:#000066;">empty</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$more_link_text</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li>
<div><span style="color:#0000FF;">$more_link_text</span> = <a href="http://www.php.net/strip_tags"><span style="color:#000066;">strip_tags</span></a><span style="color:#006600; font-weight:bold;">&#40;</span>wp_kses_no_null<span style="color:#006600; font-weight:bold;">&#40;</span><a href="http://www.php.net/trim"><span style="color:#000066;">trim</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$matches</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>&lt;/code&gt;</div>
</li>
<li>
<div>&nbsp;</div>
</li>
<li>
<div><span style="color:#0000FF;">$hasTeaser</span> = <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li>
<div><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><code>$pages</code> is some kind of global that doesn't seem to have any relation to a post.  Then apparently we're looking for HTML comments of <code>&lt;!--more something --&gt;</code>, and replacing them with... well, something.  I'd hate to think what would happen if I ever wrote a post with an HTML comment in it that happened to hit on whatever random content markers Wordpress has decided to use.  (<em>Oh wait!  That</em> <em><strong>just happened to me</strong></em> <em>while I was trying to publish the above code fragment!</em>)  I didn't even bother to look into things like <code>wp_kses_no_null</code>.  It probably involves dark rituals with live chicken sacrifice.  Why is there so much going on in a function called <strong>get</strong>_the_content()?</p>
<p>In the end, it seems that <code>get_the_content()</code> will eventually get the content of a post, but only if you set a half-a-dozen or so globals before you call it.  And <em>what the hell post is it even getting</em>?</p>
<h2>"The Loop"</h2>
<h2><img class="alignright size-medium wp-image-161" style="margin-left: 10px; margin-right: 10px;" title="the-broken-chain1" src="http://www.phpvs.net/wp-content/uploads/2009/12/the-broken-chain1-300x224.jpg" alt="the-broken-chain1" width="300" height="224" /></h2>
<p>Digging further, it's clear that the template functions for output are all like that.  They don't take any kind of parameters; <em>they just operate on globals</em>!  <strong>There's no way to take the post data that I just retrieved with <code>wp_get_recent_posts()</code>, and format it using these functions.</strong> You have to be in "The Loop" in order to do that.  And "The Loop" sucks.  It's not a catchy, easy-to-use method of handling posts, despite Wordpress's efforts to pass it off as something neat or fun.  It's a mish-mash of global functions with random naming and variable schemes (incidentally, just like the rest of Wordpress).  You can only use "The Loop" if you're accessing Wordpress in a "normal", web-requested-and-template-loaded kind of way.  It doesn't work if you're outside a template file (such as in functions.php before a template gets loaded).</p>
<p>So back to square one.  Unfortunately, it appears that if I want to have the regular blog-formatted output, I need to harness "The Loop" somehow, and clearly you can't do that on your own (ie. outside of a template file) without knowing about every global variable in the system.</p>
<p>After some quick googling, I came across the <a href="http://codex.wordpress.org/Template_Tags/query_posts" target="_blank">query_posts()</a> function, which you can use to set up "The Loop".  Reading the documentation on it, you can find this little gem:</p>
<blockquote><p>"The query_posts function overrides and replaces the main query for the page. To save your sanity, do not use it for any other purpose."</p></blockquote>
<p>To paraphrase: "We've created a public API function that is pretty much useless except in a very specific page-dependent situation.  Please enjoy how useless it is.  But don't use it."</p>
<p>The fact that there is a "main query" for a page is another indicator of just how global-happy Wordpress is, and that in turn gives you an insight into why it has so many security holes.  How do you keep track of so many globals across so many functions?</p>
<h2>A solution... sort of.</h2>
<p>Fortunately, the <code>query_posts()</code> doc page links to the <a href="http://codex.wordpress.org/Function_Reference/WP_Query" target="_blank">WP_Query docs</a>, which is marginally more helpful, and provides the path for a solution.  Using <code>WP_Query</code> sets up the wacky global stuff necessary to use "The Loop", which means we can hack our way through to getting some formatted post content.  While technically feasible, you have to emulate a bunch of <code>$_REQUEST</code> parameters to the query() method.  I ended up with this:</p>
<p><code>
<div class="igBar"><span id="lphp-4"><a href="#" onclick="javascript:showPlainTxt('php-4'); return false;">&gt;&gt; show as plain text</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-4">
<div>
<ol>
<li>
<div><span style="color:#000000; font-weight:bold;">function</span> cacheMostRecentPost<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li>
<div><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li>
<div><span style="color:#0000FF;">$featuredPosts</span> = <span style="color:#000000; font-weight:bold;">new</span> WP_Query<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li>
<div><span style="color:#0000FF;">$featuredPosts</span>-&amp;gt;query<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'showposts=1'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li>
<div><span style="color:#616100;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$featuredPosts</span>-&amp;gt;have_posts<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li>
<div><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li>
<div><span style="color:#0000FF;">$featuredPosts</span>-&amp;gt;the_post<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li>
<div><a href="http://www.php.net/ob_start"><span style="color:#000066;">ob_start</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li>
<div><span style="color:#FF9933; font-style:italic;">//do output with stuff like the_title() and the_content()</span></div>
</li>
<li>
<div><span style="color:#0000FF;">$str</span> = <a href="http://www.php.net/ob_get_contents"><span style="color:#000066;">ob_get_contents</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li>
<div><a href="http://www.php.net/ob_end_clean"><span style="color:#000066;">ob_end_clean</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li>
<div><span style="color:#FF9933; font-style:italic;">//write $str to cache fragment</span></div>
</li>
<li>
<div><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li>
<div><span style="color:#006600; font-weight:bold;">&#125;</span>&lt;/code&gt;</div>
</li>
<li>
<div>&nbsp;</div>
</li>
<li>
<div><span style="color:#FF9933; font-style:italic;">//set up hooks for this file when a post is changed or deleted</span></div>
</li>
<li>
<div>add_action<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'save_post'</span>, <span style="color:#FF0000;">'cacheMostRecentPost'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li>
<div>add_action<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'deleted_post'</span>, <span style="color:#FF0000;">'cacheMostRecentPost'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>So despite relying on a specific set up of incoming HTTP parameters (as a string) for the most part, at least you can pass paramters to the query if you know the right ones.  In this case, "showposts=1" seems to be the total number of posts fetched, and they appear to come back ordered by posting date, most recent first.  This works for what I want it to do, but guess what?  It doesn't work if you try to run it anywhere that's not one of those action hooks, because "The Loop" overwrites all the globals necessary for doing output later!  So I can't use that function, say, at the top of the index.php template file if I wanted to.  If I do, thanks to the overwritten globals, Wordpress decides that I actually want the "Archive" page instead of the index page(!), and switches templates accordingly. So while I achieved my goal of being able to cache a post to a file with this function, it's certainly not portable, and it's certainly not elegant.</p>
<h2>Wharrgarbl</h2>
<h2><img class="alignright size-medium wp-image-158" title="wharrgarbl" src="http://www.phpvs.net/wp-content/uploads/2009/12/wharrgarbl-300x240.jpg" alt="wharrgarbl" width="300" height="240" /></h2>
<p>The entire code flow is mind-boggling.  Basing the output functions around a bunch of globals reminds me of code someone would have written in PHP 3 a decade ago, or something a very inexperienced programmer would write.  Definitely not something you would expect in an application used by what is probably now millions of people.  What's wrong with having some data fetching functions, and some output functions?  You could, and I know I'm talking crazy here, but you could fetch some data, and then <em>pass it</em> to the output functions.  Then (bear with me here), you could probably fetch posts (or whatever) <em>at any time</em>, and get some formatted output <em>at any time</em>, without overwriting some important global that might be used later in the code flow.  Revolutionary, I know.  Sorry if I went too fast on that.  I'll repeat it louder and/or slower for any Wordpress core developers that happen to be reading.</p>
<p>So, Wordpress?  How about something like:</p>
<p><code>$postObjects = getRecentPostsByDate(1);<br />
$output = formatPostContent($postObject[0]);</code></p>
<p>The mere concept of having individual posts exist inside their own little encapsulated world would make the APIs a hundred times more useful (and easier to understand).  You could even keep those crap <code>the_title()</code> and <code>the_content()</code> and <code>the_something_lol_naming_scheme_lol()</code> functions if you wanted.  Just make them take parameters.  Better yet, put them inside a formatting object, or even the post object itself.  <code>$post-&gt;the_content()</code> would still work, but it would have context!</p>
<p>The reason this gets me worked up is not that it's so frustrating to use (although that helps).  I've had to deal with a lot of frustrating code in my career.  It's more the fact that it's this kind of thing that gives PHP programmers a bad name.  The code is just bad.  The design is random.  The API functions are random.  The naming schemes are random.  Functions don't do what their name (or their doc comment) indicates they should do.  Integrating wordpress into another application or site is next to impossible (try it, I dare you), and the other way around, integrating another application or site into wordpress is much more difficult than it should be.  Global usage is rampant and ridiculous to follow.</p>
<p>You don't have to look any farther than a single Wordpress code file to understand why there have been so many security holes over the last couple of years.  And there's a lot of PHP code out there that's the quality of Wordpress, or worse.</p>
<p>To re-iterate my opening, if you don't need to get anything special out of it, Wordpress does the job.  They've filled their market niche well, and it's encouraging that development is ongoing and releases occur often.  I've worked with it on occasion over the last few years, and the improvements are obvious, interface-wise especially, and to some extent code-wise as well (the WP_Query object is a step forward).   But working with the code is not fun.  Even modifying the template files is an exercise in counter-intuitiveness.</p>
<p>I'm sure there are reasons the code is what it is at this point, and I'm equally as sure I don't have the full picture to go with my condemnations.  I guess I should just be thankful that I don't have to maintain it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2009/12/08/an-exercise-in-wordpress-integration-or-why-wordpress-sucks/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>ASP.Net MVC &#8211; How to route to images or other file types</title>
		<link>http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/</link>
		<comments>http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 04:23:51 +0000</pubDate>
		<dc:creator>morgan</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/?p=123</guid>
		<description><![CDATA[A recent question on Stack Overflow (and subsequent answer that I wrote for it) inspired this post.  I had recently been discussing URL rewriting in depth with my brother, and have also been doing some introductory work with the routing engine in ASP.Net MVC, and the question piqued my interest since I had been [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.phpvs.net/wp-content/uploads/2009/08/Image.png"><img class="alignright size-full wp-image-228" style="border: 0pt none;" title="Image" src="http://www.phpvs.net/wp-content/uploads/2009/08/Image.png" alt="" width="256" height="256" /></a>A <a href="http://stackoverflow.com/questions/1146652/how-do-i-route-images-using-asp-net-mvc-routing">recent question on Stack Overflow</a> (and subsequent answer that I wrote for it) inspired this post.  I had recently been discussing URL rewriting in depth with my brother, and have also been doing some introductory work with the routing engine in ASP.Net MVC, and the question piqued my interest since I had been meaning to look at this more closely for some time.</p>
<p>The question on Stack Overflow is titled "How do I route images with ASP.Net MVC", but fundamentally the question is really asking "<strong>how can I use ASP.Net MVC to re-route URL's to actual physical files, rather than methods of a controller?</strong>"</p>
<p>To be clear, lets address the conceptual differences between routing and url rewriting.  Url rewriting takes the requested URL and modifies it before your code ever sees it.  As far as your application is concerned, the client requested the rewritten URL.  All that URL rewriting does is to change one URL into another URL, based on pattern matching.</p>
<p>Routing is a different and much more powerful beast.  The ASP.Net routing engine maps an URL to a "resource", based on a set of routes.  The first route to match the requested URL wins the prize, and sends the request off to the resource it chooses.  For the ASP.Net MVC framework (which uses <code>System.Web.Routing</code> under the hood), a resource is something that can handle the request object, which is always a piece of code.</p>
<p>So where does that leave physical files?  If a request is always parsed by the routing engine and then handed off to some function somewhere, how can we ever route a request for an image to actually return the physical image?</p>
<p>Well, it takes a tiny bit of legwork, but once we're through it, I'm confident you will see the huge advantages that routing has over simple url-rewriting.  We will show the equivalent of url-rewriting by handling a request for an image using an URL that doesn't map to a physical path, but be able to return the image anyway.</p>
<h2>Handling the Request</h2>
<p>First off, we need to handle the request that we want to re-route to a physical file.  Out of the box, ASP.Net MVC uses an instance of the <code>MvcRouteHandler </code>object to handle every request.  <code>MvcRouteHandler </code> hides all the complexities of taking the requested URL, breaking it down into parts, finding the right controller in your application, instantiating it and passing it all the data it needs.</p>
<p>The end result of <code>MvcRouteHandler </code>is not what we desire. We want to return an image, not instantiate a controller and run a method.   We want to skip dealing with controllers altogether in this case.  So lets create our own route handler that we'll use instead.</p>
<p>To do so, we simply implement <code>IRouteHandler</code>, an interface exposed by ASP.Net MVC that actually inherits from <code>IHttpHandler</code>.  This means that what we're writing is the ASP.Net MVC equivalent of an .ashx file for a webforms app - we're inserting our own handling module into the ASP.Net pipeline, that will handle the request much closer to the webserver/http level, rather than at the ASP.Net application level.</p>
<p><code>IRouteHandler </code>only has one method that we need to implement, which is <code>GetHttpHandler()</code>.</p>
<pre class="prettyprint"><code><span class="kwd">using</span><span class="pln"> </span><span class="typ">System</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">using</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="typ">Collections</span><span class="pun">.</span><span class="typ">Generic</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">using</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="pln">IO</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">using</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="typ">Linq</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">using</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="typ">Web</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">using</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="typ">Web</span><span class="pun">.</span><span class="typ">Compilation</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">using</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="typ">Web</span><span class="pun">.</span><span class="typ">Routing</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">using</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="typ">Web</span><span class="pun">.</span><span class="pln">UI</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">namespace</span><span class="pln"> MvcApplication1
</span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">ImageRouteHandler</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">IRouteHandler</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">IHttpHandler</span><span class="pln"> </span><span class="typ">GetHttpHandler</span><span class="pun">(</span><span class="typ">RequestContext</span><span class="pln"> requestContext</span><span class="pun">)</span><span class="pln">
        </span><span class="pun">{</span><span class="pln">
            </span><span class="kwd">string</span><span class="pln"> filename </span><span class="pun">=</span><span class="pln"> requestContext</span><span class="pun">.</span><span class="typ">RouteData</span><span class="pun">.</span><span class="typ">Values</span><span class="pun">[</span><span class="str">"filename"</span><span class="pun">]</span><span class="pln"> </span><span class="kwd">as</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">;</span><span class="pln">

            </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">string</span><span class="pun">.</span><span class="typ">IsNullOrEmpty</span><span class="pun">(</span><span class="pln">filename</span><span class="pun">))</span><span class="pln">
            </span><span class="pun">{</span><span class="pln">
                </span><span class="com">requestContext.HttpContext.Response.Clear();
                requestContext.HttpContext.Response.StatusCode = 404;
                requestContext.HttpContext.Response.End();
</span><span class="pln">            </span><span class="pun">}</span><span class="pln">
            </span><span class="kwd">else</span><span class="pln">
            </span><span class="pun">{</span><span class="pln">
                requestContext</span><span class="pun">.</span><span class="typ">HttpContext</span><span class="pun">.</span><span class="typ">Response</span><span class="pun">.</span><span class="typ">Clear</span><span class="pun">();</span><span class="pln">
                requestContext</span><span class="pun">.</span><span class="typ">HttpContext</span><span class="pun">.</span><span class="typ">Response</span><span class="pun">.</span><span class="typ">ContentType</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="typ">GetContentType</span><span class="pun">(</span><span class="pln">requestContext</span><span class="pun">.</span><span class="typ">HttpContext</span><span class="pun">.</span><span class="typ">Request</span><span class="pun">.</span><span class="typ">Url</span><span class="pun">.</span><span class="typ">ToString</span><span class="pun">());</span><span class="pln">

                </span><span class="com">// find physical path to image here.  </span><span class="pln">
                </span><span class="kwd">string</span><span class="pln"> filepath </span><span class="pun">=</span><span class="pln"> requestContext</span><span class="pun">.</span><span class="typ">HttpContext</span><span class="pun">.</span><span class="typ">Server</span><span class="pun">.</span><span class="typ">MapPath</span><span class="pun">(</span><span class="str">"~/test.jpg"</span><span class="pun">);</span><span class="pln">

                requestContext</span><span class="pun">.</span><span class="typ">HttpContext</span><span class="pun">.</span><span class="typ">Response</span><span class="pun">.</span><span class="typ">WriteFile</span><span class="pun">(</span><span class="pln">filepath</span><span class="pun">);</span><span class="pln">
                requestContext</span><span class="pun">.</span><span class="typ">HttpContext</span><span class="pun">.</span><span class="typ">Response</span><span class="pun">.</span><span class="typ">End</span><span class="pun">();</span><span class="pln">
            </span><span class="pun">}</span><span class="pln">
            </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">null</span><span class="pun">;</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">

        </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">string</span><span class="pln"> </span><span class="typ">GetContentType</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> path</span><span class="pun">)</span><span class="pln">
        </span><span class="pun">{</span><span class="pln">
            </span><span class="kwd">switch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">Path</span><span class="pun">.</span><span class="typ">GetExtension</span><span class="pun">(</span><span class="pln">path</span><span class="pun">))</span><span class="pln">
            </span><span class="pun">{</span><span class="pln">
                </span><span class="kwd">case</span><span class="pln"> </span><span class="str">".bmp"</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"Image/bmp"</span><span class="pun">;</span><span class="pln">
                </span><span class="kwd">case</span><span class="pln"> </span><span class="str">".gif"</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"Image/gif"</span><span class="pun">;</span><span class="pln">
                </span><span class="kwd">case</span><span class="pln"> </span><span class="str">".jpg"</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"Image/jpeg"</span><span class="pun">;</span><span class="pln">
                </span><span class="kwd">case</span><span class="pln"> </span><span class="str">".png"</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"Image/png"</span><span class="pun">;</span><span class="pln">
                </span><span class="kwd">default</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">break</span><span class="pun">;</span><span class="pln">
            </span><span class="pun">}</span><span class="pln">
            </span><span class="kwd">return</span><span class="pln"> </span><span class="str">""</span><span class="pun">;</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span></code></pre>
<p>The above <code>IRouteHandler </code>is pretty simple.  Ignoring the <code>GetContentType </code>helper method, there's really only two things happening.  First, we check for a "filename" parameter that got passed in to our handler (more on that in a second).  If it's not there, we return a 404 response.  Otherwise, we attempt to open up the physical file "test.jpg", and stream it to the browser.</p>
<p>Clearly, this should be adapted to your needs by actually using the filename parameter to find the physical files on your system.   But moving on - how do we invoke this from our MVC app?  And how do we pass in the filename parameter, of which we'd like to reroute to some other physical path?</p>
<h2>Routing the Request to the Custom Handler</h2>
<p>Well, this is the easy part.  Where you'd normally define your routes in <code>Global.asax</code>, simply use <code>routes.Add()</code>, instead of <code>routes.MapRoute()</code>.  Just like this:</p>
<pre>routes.Add("ImagesRoute",
                 new Route("graphics/{filename}", new ImageRouteHandler()));</pre>
<p>This method of adding our route allows us to specify our custom <code>IRouteHandler</code>, rather than <code>routes.MapRoute()</code>, which by default uses an instance of <code>MvcRouteHandler</code>.  So now, we've defined a route that matches against any requested URL containing "graphics/", and puts the rest of the URL into the "filename" bucket of the <code>RouteDataDictionary</code>, and hands it off to our <code>IRouteHandler</code>.  This is how we pass the filename parameter into our custom route handler - basically the same way we pass things into controllers, by defining the variables in the route pattern.</p>
<p>We've successfully routed all URL's containing "graphics/", which doesn't physically exist in our web application, and returning "temp.jpg", which could exist anywhere.  With a bit of coding around the file IO, you could return files from anywhere.</p>
<p>And that's pretty much it!  You might be thinking, "this seems like a lot of extra work just to re-route a URL to a physical file that already existed in my web app!".   If you take a step back though, you'll see the power of this approach.  What if you wanted to log every request to the original URL to a special log file?  What if you wanted to also transform the image before returning it?  Perhaps launch a system executable or asynchronously hit a web service?  What if you wanted to...?</p>
<p>In a nutshell, by inserting your own HttpHandlers into the ASP.Net pipeline to handle routed requests, you can code <em>anything that you'd like to happen</em> when a request comes in, rather than just rewriting it to some other URL.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.phpvs.net%2f2009%2f08%2f06%2faspnet-mvc-how-to-route-to-images-or-other-file-types%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.phpvs.net%2f2009%2f08%2f06%2faspnet-mvc-how-to-route-to-images-or-other-file-types%2f&amp;bgcolor=FF9933&amp;cbgcolor=D4E1FD" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Facebook Notifications &#8211; &quot;An unknown error occurred (out of memory)&quot;</title>
		<link>http://www.phpvs.net/2009/07/19/facebook-notifications-an-unknown-error-occurred-out-of-memory/</link>
		<comments>http://www.phpvs.net/2009/07/19/facebook-notifications-an-unknown-error-occurred-out-of-memory/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 18:54:04 +0000</pubDate>
		<dc:creator>blake</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/?p=103</guid>
		<description><![CDATA[Over the last few months, I've worked with the Facebook Notification system many times, and there has always been a moderate-to-high level of frustration with it.  It is difficult to test on a development application, because you simply don't have the same number of users as a live application, and the application settings on [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last few months, I've worked with the Facebook Notification system many times, and there has always been a moderate-to-high level of frustration with it.  It is difficult to test on a development application, because you simply don't have the same number of users as a live application, and the application settings on a dev app are different, which affects your allocation of allowed notifications.</p>
<p>One of the prevalent errors I was getting for a long time was that a notification sent to a large number of users (application-to-user notification) would invariably fail with an Exception thrown from the Facebook PHP client library.  My notification controller code in a particular application was set up to catch Exceptions and display the error into the administration control panel.  However, "<strong>An unknown error occurred (out of memory)</strong>" is about as helpful as you might imagine.  Yay Facebook.  The only thing going for this particular error is that it's an iota more helpful than Facebook's documentation, which you quickly learn to distrust as out-of-date or just plain wrong.</p>
<h3><strong>The logic</strong></h3>
<p>My notification function logic was fairly simple:  Accept an array of Facebook user ids, run an FQL query against Facebook to make sure the users all had the application installed, remove any ids that didn't, then use the Notifications.send() method (via the PHP client library's <code>$facebook-&gt;api_client-&gt;notifications_send()</code> method) to fire off the message.</p>
<p>No problems on development.  On production, it was a giant party of "An unknown error occurred (out of memory)" errors.</p>
<p>At first I thought that Notifications.send() couldn't handle a lot of user ids (I was giving it at least 5000), so I put a loop in to break it up into several small "chunk sends".  I went all the way down to 500 users per request, and nothing seemed to change, other than I was probably wasting my allocated notifications.  The only silver lining to doing this testing on a production server was that the failed notifications didn't seem to go out to anyone, although that's impossible to tell for sure.</p>
<h3><strong>It's not the size of the notification, it's how you verify it.</strong><span style="text-decoration: underline;"><strong><br />
</strong></span></h3>
<p>I struggled with this for a long while until I realized that there was only one other possible location for Facebook to throw this error, and that was with the FQL query.  I moved my "chunking" loop to work with the FQL as well, and I finally had relief from the "unknown" error.  My conclusion therefore is that <strong>FQL queries <em>might not</em> be able to handle a large result set</strong>.</p>
<p>Since the documentation and forums couldn't give me any help, I thought I'd post my working code and explanation here, in the hopes that someone might find it useful.</p>
<p>Note that the Facebook object has been previously set as a property of the Controller that is executing this function, so it is accessed via <code>$this-&gt;facebook</code> here.  You can see how the original array is broken into separate requests of approximately 500 users each, with each chunk of users being verified in turn.  I have no idea what a safe number of users would be, however 500 seems to be working for me (for now).</p>
<pre><code>/**
 * Sends a Facebook Notification message to all given users of an application.
 *
 * @param array $facebookIds Array of Facebook IDs to send the notification to.
 * @param string $msg Notification message to send.
 * @return int Count of how many users were sent the message.
 * @link http://wiki.developers.facebook.com/index.php/Notifications.send
 */
public function notifyMultipleUsers($facebookIds, $msg)
{
	if (! is_array($facebookIds) || count($facebookIds) == 0) {
		throw new Exception('No user ids given to notifyMultipleUsers().');
	}
	$reqSize = 500;
	$numUsers = 0;
	while(count($facebookIds) &gt; 0) {
		$tmpIds = array_splice($facebookIds,0,$reqSize);
		//Check against Facebook database to verify which users have the app installed.
		$fql = 'SELECT uid FROM user '.
		       'WHERE is_app_user=1 AND uid IN ('.implode(',',$tmpIds).')';
		$fqlRes = $this-&gt;facebook-&gt;api_client-&gt;fql_query($fql);
		$appUsers = array();
		foreach($fqlRes as $a) {
			$appUsers[] = $a['uid'];
		}
		$c = count($appUsers);
		if ($c &gt; 0) {
			$this-&gt;facebook-&gt;api_client-&gt;notifications_send(
				implode(',',$appUsers),$msg,'app_to_user'
			);
			$numUsers += $c;
		}
	}
	return $numUsers;
}</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2009/07/19/facebook-notifications-an-unknown-error-occurred-out-of-memory/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>.Net Mocking Frameworks &#8211; Capability Comparison</title>
		<link>http://www.phpvs.net/2009/04/25/net-mocking-frameworks-capability-comparison/</link>
		<comments>http://www.phpvs.net/2009/04/25/net-mocking-frameworks-capability-comparison/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 06:04:40 +0000</pubDate>
		<dc:creator>morgan</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Integration Testing]]></category>
		<category><![CDATA[Mocks]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/?p=80</guid>
		<description><![CDATA[A chart form summary of some of the features, capabilities and characteristics of four of the most popular mock object frameworks used for test driven development and design with .Net projects.]]></description>
			<content:encoded><![CDATA[<p>I have a couple years of experience of TDD under my belt, but it's only recently that I've felt like I am a relatively decent practitioner of it.  I attribute this to forcing myself to take the plunge into mocking, and the knowledge of patterns and loosely-coupled design that I've gained from it.</p>
<p>You see, I work on a pretty large and complex ASP.Net webforms product, and tests were introduced late into the development cycle of the initial release.  We favored integration testing with real data sources over actual unit tests.  While I did write some tests, I knew that our product was not very testable by design.</p>
<p>Recently we put together a public facing API for programming against the product, that we were able to build from scratch.  This was a natural opening to apply test driven practices and start building unit tests from the get-go.  Due to the service oriented nature of the data that the product consumes, I soon found myself realizing that I needed mocks in a big way.  I gritted my teeth and dove head-first into Rhino.mocks.</p>
<p>Several weeks down the road, it was obvious that Rhino just isn't right for our environment.  The learning curve is too steep to win quick success with all our developers (and therefore by extension, our project managers).  I began looking for another framework.</p>
<p>This led me on a frustrating research mission to find the differences between frameworks without actually trying them all. Due to a lack of in-depth comparisons of the actual capabilities of the various mocking frameworks, I've ended up putting together this chart.  I'm hoping it helps some people.  My interest is very high in this arena, so I will be keeping it up to date.</p>
<p>(Just for interest's sake - we ended up choosing Moq, due to the ease of the API.   The philosophy of Moq jives perfectly with our relatively fast-paced and efficient development environment.  We don't care about purism - we care about getting it done.)</p>
<div id="attachment_98" class="wp-caption aligncenter" style="width: 650px"></p>
<div class="mceTemp mceIEcenter">
<dl id="attachment_101" class="wp-caption aligncenter" style="width: 650px;">
<dt class="wp-caption-dt"><img class="size-full wp-image-101" title=".Net Mocking Frameworks" src="http://www.phpvs.net/wp-content/uploads/2009/05/comparison.png" alt=".Net Mocking Framework Comparison" width="640" height="488" /><p class="wp-caption-text">.Net Mocking Framework Comparison </p></div>
</dt>
</dl>
</div>
<p style="text-align: center;"><strong>Other Notes</strong></p>
<table border="1" cellspacing="0" cellpadding="0" width="690" align="center">
<tbody>
<tr>
<td width="330" valign="top"><strong>Rhino</strong></p>
<ul class="unIndentedList">
<li> Mature, flexible framework</li>
</ul>
<ul class="unIndentedList">
<li> Built on Castle DynamicProxy</li>
</ul>
<ul class="unIndentedList">
<li> Very large array of "syntaxes" leads to extreme   confusion when writing tests - documentation mixed for the new 3.5 fluent syntax</li>
</ul>
<ul class="unIndentedList">
<li> Large community of users</li>
</ul>
</td>
<td width="360" valign="top"><strong>Moq</strong></p>
<ul class="unIndentedList">
<li> New(ish) framework also built on Castle</li>
</ul>
<ul class="unIndentedList">
<li> Requires .Net 3.5 due to its lambda heavy   syntax</li>
</ul>
<ul class="unIndentedList">
<li> No distinction between mocks &amp; stubs,   record/playback (joy!!)</li>
</ul>
<ul class="unIndentedList">
<li> Responsive and active developers and community</li>
</ul>
</td>
</tr>
<tr>
<td width="330" valign="top"><strong>NMock2</strong></p>
<ul class="unIndentedList">
<li> Uses "magic strings" for mocking - makes tests   brittle</li>
</ul>
<ul class="unIndentedList">
<li> Confusing product version #'s - Nmock2 is   actually a new team that picked up NMock and continued development</li>
</ul>
</td>
<td width="360" valign="top"><strong>TypeMock.Net</strong></p>
<ul class="unIndentedList">
<li> Powerful framework that uses redirection at   the IL level to create mocks</li>
</ul>
<ul class="unIndentedList">
<li> Expensive ($450/license)</li>
</ul>
<ul class="unIndentedList">
<li> TDD Purists argue that the power of it leads   to poorer design</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>The above observations were gleaned from many web pages, documentation repositories and blog posts, and reflect my limited understanding of each framework.  <em><strong> There may be errors</strong></em>.   If you think I'm wrong, or you can think of other capability aspects that I've overlooked, please let me know!</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.phpvs.net%2f2009%2f04%2f25%2fnet-mocking-frameworks-capability-comparison%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.phpvs.net%2f2009%2f04%2f25%2fnet-mocking-frameworks-capability-comparison%2f&amp;bgcolor=FF9933&amp;cbgcolor=D4E1FD" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2009/04/25/net-mocking-frameworks-capability-comparison/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fixing &quot;Error 320 (net::ERR_INVALID_RESPONSE): Unknown error&quot; with Google Chrome</title>
		<link>http://www.phpvs.net/2009/03/26/fixing-error-320-neterr_invalid_response-unknown-error-with-google-chrome/</link>
		<comments>http://www.phpvs.net/2009/03/26/fixing-error-320-neterr_invalid_response-unknown-error-with-google-chrome/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 16:29:11 +0000</pubDate>
		<dc:creator>blake</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[LAMP]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/?p=75</guid>
		<description><![CDATA[Without starting a browser flamewar, I'll just say that Google Chrome is a nice idea, but I won't be switching any time soon.  As software evolves, competitors always appear, distilling out the good parts of existing products to create their take on something, and make it better.  Google has done that with Chrome; [...]]]></description>
			<content:encoded><![CDATA[<p>Without starting a browser flamewar, I'll just say that Google Chrome is a nice idea, but I won't be switching any time soon.  As software evolves, competitors always appear, distilling out the good parts of existing products to create their take on something, and make it better.  Google has done that with Chrome; it's light and fast and responsive.  Unfortunately, it's still a bit green on the battlefield.</p>
<p>Take this particular error for instance:  <strong>Error 320 (net::ERR_INVALID_RESPONSE): Unknown error</strong>.  Have you seen this?  I was emailed the other day by someone who was browsing a site I am responsible for that "the site is down!  when will it be fixed?!".  Seeing that it was fine in all my "normal" test browsers (Firefox, Safari, Opera, IE6, IE7), I gritted my teeth against the inevitable email conversation to follow that would be necessary to get even the slightest helpful detail out of this person.</p>
<p>Eventually, it came out that this person was using Chrome, and sure enough, I immediately got the same error when I fired it up.  Not only was this particular site down, but every site on the server gave the same response.  Chrome would not load a single page from any site, complaining only that "Error 320 (net::ERR_INVALID_RESPONSE): Unknown error" was upon it, like some kind of HTTP Götterdämmerung.</p>
<p>Googling wasn't immediately helpful; mostly forum threads complaining about the same problem, but no real fixes were presented.  Amusingly, it seemed that at one time a lot of Chrome users couldn't use Chrome to access Gmail because of this error.  However, I managed to find <a href="http://code.google.com/p/chromium/issues/detail?id=138">a thread on Google code</a>  which turned out to be somewhat enlightening.  The problem appears to be in the way that Chrome handles HTTP headers, where certain forms of some HTTP headers will trigger the error, causing Chrome to pack it in.  To be fair, it's really a Windows/Microsoft problem, as the bug is apparently in the underlying <a href="http://msdn.microsoft.com/en-us/library/aa384273(VS.85).aspx">WinHTTP</a> service, which Chrome utilizes.</p>
<p>The good news is that the Google devs have apparently solved the problem by switching away from WinHTTP, and it has apparently been fixed in the development branches already.  Unfortunately, that's only good news if your average user has downloaded the development version of Chrome, and not if you're a website owner and someone is complaining about your site.</p>
<p>Armed with the knowledge that it was a header problem, without doing any further research I went ahead and upgraded Apache.  The server in question had been running version 2.2.6, and I upgraded to Apache 2.2.11.</p>
<p>I was extremely pleased to discover that this <strong><em>actually fixed the problem</em></strong>.  Chrome immediately worked just fine, and I immediately cracked open a self-congratulatory beer.</p>
<p>Without examining the Apache changelogs to discover what might have fixed it, I'm happy to just give kudos to those people that deal with that level of the web, and were clever enough to change/fix whichever header it was (I have a hunch it was a mod_rewrite issue, but I'm talking out my posterior on that one).  That being said, Chrome (and WinHTTP) should be able to handle invalid HTTP headers, especially if it's an unimportant one and the page content can be salvaged.  It's the web... things get garbled and junky all the time (have you ever seen a MySpace page?)</p>
<p>Unfortunately, the end result of this is that now I have <strong>yet another browser</strong> that I have to check things with when doing web development.</p>
<p>Just what I wanted Chrome to be.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2009/03/26/fixing-error-320-neterr_invalid_response-unknown-error-with-google-chrome/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Defend PHP</title>
		<link>http://www.phpvs.net/2009/03/21/defend-php/</link>
		<comments>http://www.phpvs.net/2009/03/21/defend-php/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 04:27:47 +0000</pubDate>
		<dc:creator>morgan</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/?p=71</guid>
		<description><![CDATA[I ran across a great discussion at StackOverflow today that started with this question:
I made a tongue-in-cheek comment in another question thread calling PHP a terrible language and it got down-voted like crazy. Apparently there are lots of people here who love PHP.
So I'm genuinely curious. What am I missing? Why makes PHP a good [...]]]></description>
			<content:encoded><![CDATA[<p>I ran across <a title="Convince me that PHP isn't horrible" href="http://stackoverflow.com/questions/309300/defend-php-convince-me-it-isnt-horrible" target="_blank">a great discussion at StackOverflow</a> today that started with this question:</p>
<blockquote><p>I made a tongue-in-cheek comment in another question thread calling PHP a terrible language and it got down-voted like crazy. Apparently there are lots of people here who love PHP.</p>
<p>So I'm genuinely curious. What am I missing? Why makes PHP a good language?</p></blockquote>
<p>The article lists a dozen or so "flaws" with the language, and then continues:</p>
<blockquote><p>Worst of all, PHP convinces people that designing web applications is easy. And it does indeed make much of the effort involved much easier. But the fact is, designing a web application that is both secure and efficient is a very difficult task.</p>
<p>By convincing so many to take up programming, PHP has taught an entire subgroup of programmers bad habits and bad design. It's given them access to capabilities that they lack the understanding to use safely. This has led to PHP's reputation as being insecure.</p></blockquote>
<p>Are the flaws in PHP really any different from any other language?</p>
<p>I'm a C# programmer, and I have to disagree with what some of what the original poster asserted were flaws with PHP.  Overly broad implicit type conversions?  Too easy to couple presentation with logic?  Heck, those are the reasons it's so popular in the first place!  Making web programming easy is a flaw?  Wut?</p>
<p>After wrestling with the back and forth in that discussion, I came to a simple conclusion.    This person has fallaciously concluded that PHP is a bad language based on his perception of its compliance with ivory-tower, computer science academia concepts.   Most of his criteria could basically describe Perl, Javascript, VBScript, heck, even VB6, HTML and C.</p>
<p>Of course PHP isn't a horrible language.  It's simply a tool, and based on its wild popularity and simple learning curve, only a fool could conclude that it was a bad one.  Even if it breeds more "unsafe programmers" (which I doubt highly), it's simply not the language's responsibility to restrict what you can do just because you might do it.</p>
<p>Every programmer should be able to see that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2009/03/21/defend-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ASP.Net Load Testing and Optimization Toolkit &#8211; So you want to be a hero</title>
		<link>http://www.phpvs.net/2008/08/11/aspnet-load-testing-and-optimization-toolkit-so-you-want-to-be-a-hero/</link>
		<comments>http://www.phpvs.net/2008/08/11/aspnet-load-testing-and-optimization-toolkit-so-you-want-to-be-a-hero/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 20:52:49 +0000</pubDate>
		<dc:creator>morgan</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[optimization asp.net load testing performance toolkit]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/?p=56</guid>
		<description><![CDATA[An overview of 9 types of tools every developer needs to know when optimizing ASP.Net applications.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.phpvs.net/wp-content/uploads/2008/08/Alpha-Dista-Icon-14.png"><img class="size-full wp-image-230 alignleft" style="margin-right: 1em; border: 0pt none;" title="Tools" src="http://www.phpvs.net/wp-content/uploads/2008/08/Alpha-Dista-Icon-14.png" alt="" width="255" height="255" /></a>One of my passions is optimization.  There's no code related task I like more than making something run better, faster, snappier - from tweaking UI registry keys to stripping out crap code - I want results.   Usually if something is noticeably slow on the user's end, there's something fundamentally wrong that can be made faster - a lot faster.</p>
<p>Why do I love optimization?  It's because it's the opposite of a thankless job.  Everyone thanks you.  Your sales team thanks you.  Project Managers thank you. Executive management drools over your optimization report - they can take it straight to the bank.  Product leads take your report and put it into the release notes.  Or a press release.  You're the hero!  And it's downright fun.</p>
<p>Long story short, my team was recently assigned optimization targets for our app for increasing user capacity (+30%) and decreasing startup and postback times (-20%).  During the iteration, the rest of the team was sucked away to emergency projects, and I singlehandedly blew so far past the targets I couldn't even see them anymore.  Final optimizations included 100% increased concurrent user load and a 97% reduction in postback execution time.  Application startup time was decreased by 40%.  Total code changes was 40 lines.  Call it precision strike coding.  Toss in one nicely formatted "before and after" optimization report, and I'd say it's been a pretty good couple of weeks.</p>
<p>But you need to know what you're doing, and where to get started.  You can't simply sit down in front of the code and start randomly caching things or turning every '+' operator into a StringBuilder. (Note: that's not a good idea anyway).  But if you get familiar with the following tools, you'll already know where to start.  So without further ado...</p>
<h2>The ASP.Net Optimizer's Toolbox</h2>
<p>If you want to optimize, you need to have an arsenal of tools to troubleshoot and profile your app.  You should be familiar with as many of these tools as possible.</p>
<ol>
<li class="olliemph">Code Profiler</li>
<p class="pemph"><strong>Recommended</strong>: <a href="http://www.red-gate.com/products/ants_profiler/index.htm">RedGate Ants (14-day free trial, $295 to purchase)<br />
</a></p>
<p>Pretty much no exceptions here - you have to have a profiler.  It's extremely time consuming and ugly to start adding TimeSpan calculations around every block of code in your app.  For the amount of time it would take to track down one slow line of code "manually", you could have run four profiler sessions and have your top ten "target list" ready to go, made yourself a sandwich and played a game of foosball.  Not only that, but profilers will identify fast blocks of code that are getting executed ridiculous numbers of times, making them slow overall.</p>
<p>Since I've used RedGate's Ants Profiler pretty much exclusively, that's all I can recommend here, but others exist.  RedGate is great at profiling code, but I find their memory profiling to be pretty lackluster, so if I need to dig into memory I'll generally fire up Windbg (see further down).</p>
<li class="olliemph">Application Stress Tool</li>
<p class="pemph"><strong>Recommended</strong>: <a href="http://www.neotys.com/">NeoLoad (10 "virtual user" Free Trial but fairly expensive to purchase)</a><br />
<strong>Also good</strong>: <a href="http://www.microsoft.com/technet/archive/itsolutions/intranet/downloads/webstres.mspx?mfr=true">WAST (Free)<br />
</a></p>
<p>You need to be able to load test your application to see how it performs.  When does it crash?  How many users does it support?  How does your memory, disks, network and processors hold up?  Load testing uncovers errors you couldn't even dream of by looking at your code - concurrency issues, how your application performs when resources are unavailable, etc.</p>
<p>Load testing tools pose a problem however, because there is a huge gap between free tools and proprietary tools.  Here's a list of some of the ones I know for Windows/IIS:</p>
<p><strong>TinyGet (Free) </strong>- run some HTTP requests sequentially in a loop.  Part of IIS 6 Resource Toolkit<br />
<strong>WAST (Free)</strong>- an oldie but a goodie - record basic web browsing and simulate lots of users<br />
<strong>MS ACT - </strong>(Application Center Test) - Next generation from WAST, allows for dynamic variables.  It comes with Enterprise Edition level MSDN subscription.  Thanks to mike for pointing it out.<br />
<strong>NeoLoad </strong>- good bang for the buck, all the functionality you'll need for even complex AJAX applications, but not cheap and has a predatory pricing model shared by higher end software<br />
<strong>Visual Studio Team Edition for Testers</strong> - you can buy a lot of NeoLoad virtual users for this kind of cash.<br />
<strong>HP LoadRunner</strong> - you could probably hire a thousand testers, buy them all computers and have them generate load manually for what you'd pay for this kind of software.</p>
<p>The problem is that the free stuff (TinyGet, WAST) is next to useless for testing sessions and AJAX apps, they're just not built to handle it.  I've recently gotten a lot of experience using Neoload and it's pretty good software with fantastic support behind it - you can record complex user sessions with many dynamic parameters, multiple viewstates and have multiple recorded sessions make up a "population" of users to test your app under a broad range of conditions.  It also hooks into the perfmon API to give you flexible reporting and graphs.  Unfortunately they charge per "virtual user" you want to have running, and it's not real cheap.  Small businesses may find it worthwhile depending on the situation.  Freelancers may want to stick with WAST, look at some other options, or roll their own.</p>
<p>The top range stuff is for medium-very large businesses that can shell out the dough, and I don't have any experience with them.  For the money, they'd better be good!</p>
<li class="olliemph">Performance Monitor</li>
<p class="pemph"><strong>Recommended</strong>: Performance Monitor (perfmon.exe) (Free)<br />
<strong>Also good</strong>: <a href="http://www.neotys.com/">NeoLoad</a></p>
<p>At what point does your app spike the CPU?  How's that CLR request queue going?  What's your heap memory look like?  Microsoft's perfmon API is very rich and if you don't have a custom app that hooks into it already, simply type "perfmon.exe" at the command prompt and you have everything you need in Microsoft's simple MMC app.  You can monitor as many machines as necessary all in the same window or log.  Experiment with recording counter log files and replaying them - this data can be exported and included for that extra-demanding CTO.  If you're unfamiliar with perfmon, play around with it - it's not too hard to figure out even though the interface is a bit dated.</p>
<li class="olliemph">HTTP Recorder</li>
<p class="pemph"><strong>Recommended</strong>: <a href="http://www.fiddler2.com/fiddler2/">Fiddler2 (Free)<br />
</a></p>
<p>Optimizing your AJAX app?  Confused about how chatty your application is being? You need to be able to see exactly what's going between client and server - requests, responses, headers, what's compressed and what isn't.  Enter Fiddler2 - this little proxy server sits between IE and any web page and records all the traffic, and reports it for you in an easy to see format.  Tip:  if you're looking at your pages on your local machine, use http://machinename instead of http://localhost, since IE ignores going through a proxy for localhost addresses.</p>
<p>Fiddler2 is especially useful for ASP.Net apps because it's not always clear exactly what kind of HTML is being generated by webcontrols.</p>
<li class="olliemph">Interface Manipulation</li>
<p class="pemph"><strong>Recommended</strong>: <a href="https://addons.mozilla.org/en-US/firefox/addon/1843">Firebug (Free)</a><br />
<strong>Also good</strong>: <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=E59C3964-672D-4511-BB3E-2D5E1DB91038&amp;displaylang=en">Developer Toolbar for IE (Free)<br />
</a></p>
<p>Any web developer by now has heard of the Firebug plug-in for Firefox. It lets you manipulate CSS, HTML and javascript on the fly on any webpage you're viewing, complete with a javascript debugger and DOM explorer.  In fact, I don't know what we did without it, back in the good 'ol Web 1.5.6.18 days.  If you must develop with IE (*shudder*) you can get the Developer Toolbar for IE, which isn't nearly as good, but does the job in a pinch.</p>
<li class="olliemph">Debugger</li>
<p class="pemph"><strong>Recommended</strong>: <a href="http://www.microsoft.com/whdc/devtools/debugging/default.mspx">Windbg (Free)</a><br />
<strong>Also good</strong>: <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=28bd5941-c458-46f1-b24d-f60151d875a3&amp;displaylang=en">DebugDiag (Free)<br />
</a></p>
<p>A good debugger/memory dump analyzer is probably the single most powerful tool in your arsenal if you know how to use it.  Windbg will let you take a snapshot of your application as it's running or when it crashes, and you can quickly narrow down what's causing crashes, hangs, excessive memory usage, or identify potential optimizations.  You can explore the runtime stacks, native and CLR, see what every thread was doing, and see what's in every memory address on your heaps.   If you've never used Windbg, there are <a href="http://blogs.msdn.com/tess/pages/net-debugging-demos-information-and-setup-instructions.aspx">fantastic lab-style tutorials</a> at <a href="http://blogs.msdn.com/tess/">Tess' blog</a> at MSDN.  These tutorials will also introduce you to perfmon and tinyget.</p>
<li class="olliemph"><a href="http://code.msdn.microsoft.com/codeanalysis/Release/ProjectReleases.aspx?ReleaseId=553">FXCop (Free)<br />
</a></li>
<p>FXCop can be a useful tool but depending on your environment, it might be more trouble than it's worth.  Simply put, it scans your code looking for bad coding practices and comes up with a report about how sloppy you are.  Unfortunately, some of the default rules may not apply to your development environment, but with a little effort you can certainly weed out the rules you don't need and make it more useful for your team.</p>
<li class="olliemph">Viewstate Decoder</li>
<p class="pemph"><strong>Recommended</strong>: <a href="http://www.pluralsight.com/community/media/p/51688.aspx">Fritz Onion's Viewstate Decoder (Free)<br />
</a></p>
<p>At some point most ASP.Net apps undergo a Viewstate optimization.  Let's face it, Viewstate can be downright disgusting.  If you're jumping into the middle of an application, it's helpful to have some guidance on where the hell it's all coming from.  Just copy and paste it into a Viewstate decoder and you might find a ton of clues.</p>
<li class="olliemph">Text editor</li>
<p class="pemph"><strong>Recommended</strong>: <a href="http://www.ultraedit.com/">Ultraedit (45 day free trial, $49.95 to buy)</a><br />
<strong>Also good</strong>: <a href="http://www.flos-freeware.ch/notepad2.html">Notepad2 (Free)<br />
</a></p>
<p>As every experienced coder knows, sometimes, you just don't want to look at something in your IDE.  You just want to open a file fast and be done with it.  I've used UltraEdit for a long time and it has great features, useful for optimization.  For example, you can copy a recorded request from Fiddler, paste it into Ultraedit, and immediately count how many times your ultra-long image pathname occurs, and immediately know how many bytes you can shave out of your file.  It has quick and easy syntax highlighting for pretty much any programming language, line numbers, and tracks the file and asks to reload it if it detects changes.  It also detects blocks of code or xml for expanding/collapsing, and hundreds of other features.</ol>
<p>Although I didn't include<a href="http://www.aisto.com/roeder/dotnet/"> Lutz Roeder's Reflector</a> in the list since I don't find that I use it that often when I'm debugging, it's something you should always have for easy reference, especially if you're dealing with unfamiliar assemblies.</p>
<p>Good luck optimizing!  Remember to spellcheck your optimization report, and don't forget to put in a one page executive summary of what a hero you are.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2008/08/11/aspnet-load-testing-and-optimization-toolkit-so-you-want-to-be-a-hero/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>PHP Variable Test reference</title>
		<link>http://www.phpvs.net/2008/06/29/php-variable-test-reference/</link>
		<comments>http://www.phpvs.net/2008/06/29/php-variable-test-reference/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 02:08:33 +0000</pubDate>
		<dc:creator>blake</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/?p=54</guid>
		<description><![CDATA[I thought I'd post a link to this PHP Variable Tests reference page.  It's a great reference that's kept up to date with the current version of PHP.  I use it sometimes when I'm waffling over what function to use to validate a variable.
Something I've noticed lately with the newer versions of PHP [...]]]></description>
			<content:encoded><![CDATA[<p>I thought I'd post a link to this <a href="http://www.killersoft.com/misc/php_variable_tests.php" target="_blank">PHP Variable Tests</a> reference page.  It's a great reference that's kept up to date with the current version of PHP.  I use it sometimes when I'm waffling over what function to use to validate a variable.</p>
<p>Something I've noticed lately with the newer versions of PHP is that <code>ctype_digit</code> no longer returns <code>true</code> when you give it an empty string (ie. <code>ctype_digit('')</code>).  This is great, since I always thought that returning true on an empty string was counter-intuitive; by definition, it should only return true "if every character in <code>$text</code> is a decimal digit", so if there's no characters, it can't be true.  It's also great because that means that there's lots of places in some of my code where I can change </p>
<p><code>if (ctype_digit((string) $x) &#038;& $x != '') </code> </p>
<p>to just </p>
<p><code>if (ctype_digit((string) $x))</code>,</p>
<p>which is cleaner and nicer.</p>
<p>It looks like they may have made this change back around PHP 5.1, but I never noticed it until I checked that variable reference page.  Nice to see it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2008/06/29/php-variable-test-reference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ten PHP Best Practices Tips that will get you a job</title>
		<link>http://www.phpvs.net/2008/06/04/ten-php-best-practices-tips-that-will-get-you-a-job/</link>
		<comments>http://www.phpvs.net/2008/06/04/ten-php-best-practices-tips-that-will-get-you-a-job/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 22:06:09 +0000</pubDate>
		<dc:creator>blake</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[PHP best practices]]></category>
		<category><![CDATA[PHP tips]]></category>
		<category><![CDATA[top ten]]></category>

		<guid isPermaLink="false">http://www.phpvs.net/2008/06/04/ten-php-best-practices-tips-that-will-get-you-a-job/</guid>
		<description><![CDATA[The last couple of weeks have been quite the experience for me.  I was part of a big layoff at my former company, which was interesting.  I've never been in that position before, and it's hard not to take it personally.  I started watching the job boards, and a nice-looking full-time PHP [...]]]></description>
			<content:encoded><![CDATA[<p>The last couple of weeks have been quite the experience for me.  I was part of a big layoff at my former company, which was interesting.  I've never been in that position before, and it's hard not to take it personally.  I started watching the job boards, and a nice-looking full-time PHP position caught my eye, so I sent out a resume and landed an interview.  Before the face-to-face portion, I chatted with the owner and head programmer on a conference call, and they ended up sending me a technical assessment quiz.  One particular question caught my eye on this quiz... it looked something like this:</p>
<p>Find the errors in the following code:
<pre>
&lt;?
function baz($y $z) {
	$x = new Array();
	$x[sales]  = 60;
	$x[profit] = 20:

	foreach($x as $key = $value) {
		echo $key+" "+$value+"&lt;BR&gt;";
	}
} 

?&gt;</pre>
<p>So, give it a shot.  How many can you find?</p>
<p>If you got the missing comma in the parameter list, the "new Array()" error, the colon instead of a semi-colon, the '=' instead of '=&gt;' in the foreach statement, and the erroneous use of '+' on the echo line, then congratulations, you found all the errors!  You have the basic PHP technical skills to pay the bills.</p>
<p>That's not how I answered the question though.  I noted the errors, obviously, but I went further than that.  For instance, did you notice that there were no single quotes around the array indexes ($x[sales] and $x[profit])?  That won't cause a fatal PHP error, but it is a coding error!  Did you also notice the use of double-quoted strings instead of single-quoted strings on the echo line?  Or the usage of the opening PHP short tag? Or the usage of "&lt;BR&gt;" instead of "&lt;br/&gt;"?</p>
<p>After pointing out the actual errors, I made a point of adding comments about those things I just mentioned.  It was enough to push the answer from "correct" to "impressive", and it scored me a lot of points with the programmers who were reviewing my application.  Enough so that they offered me the job!  (I eventually turned it down, as I have been seduced by the siren call of the contracting life, and I intend to flex my PHP skills to the benefit of my clients, and not a faceless corporate overlord who dabbles in telemarketing.  I need a shower).</p>
<p>So, read on for my Ten PHP Best Practices Tips that will get you a job:</p>
<p><strong>1.  Single-quoted strings are your friend.</strong> When you surround a PHP string in double quotes, it is subsequently parsed by the PHP interpreter for variables and special characters, such as "\n".  If you just want to output a basic string, use single quotes!  There is a marginal performance benefit, since the string does not get parsed.  If you have variables or special characters, then by all means use double-quotes, but pick single quotes when possible.</p>
<p><strong>2.  String output.</strong> Which line of code do you think runs faster?<br />
<code><br />
print "Hi my name is $a. I am $b";<br />
echo "Hi my name is $a. I am $b";<br />
echo "Hi my name is ".$a.". I am ".$b;<br />
echo "Hi my name is ",$a,". I am ",$b;<br />
</code><br />
This might seem weird to you, but the last one is actually the fastest operation.  print is slower than echo, putting variables inline in a string is slower than concatenating them, and concatenating strings is slower than using comma-separated echo values! Not only does not-inlining your variables give you a performance boost, but it also makes your code easier to read in any editor that has syntax highlighting (your variables will show up in nice colors).  The little-known use of echo as a function that takes a comma-separated list of values is the fastest of them all, since no string operations are performed, it just outputs each parameter.  If you combine all this with Tip #1 and use single quotes, you're on your way to some finely-tuned strings.</p>
<p><strong>3.  Use single-quotes around array indexes.</strong> As you saw in the quiz question above, I pointed out that <code>$x[sales]</code> is technically incorrect!  You should quote associative array indexes, like so: <code>$x['sales']</code>.  This is because PHP considers the unquoted index as a "bare" string, and considers it a <em>defined constant</em>.  When it can't find a matching symbol for this constant in the symbol table however, it converts it to a real string, which is why your code will work.  Quoting the index prevents this constant-checking stuff, and makes it safer in case someone defines a future constant with the same name.  I've also heard that it is up to seven times faster than referencing an unquoted index, although I haven't tested this.  For more on this, see the section called "Array do's and don'ts" in the <a href="http://www.php.net/manual/en/language.types.array.php" target="_blank">Array section</a> of the PHP manual.</p>
<p><strong>4.  Don't use short open tags.</strong> Eww... are you really using these?  <code>&lt;?</code> is just bad form.  It can cause conflicts with XML parsers, and if you ever distribute code, it's going to annoy the heck out of people who have to start modifying their PHP ini directives to get it to work.  There's just no good reasons to use short open tags.  Use the full <code>&lt;?php</code>.</p>
<p><strong>5.  Don't use regular expressions if you don't need to.</strong> If you're doing basic string operations, stay away from the preg and ereg function groups whenever possible.  str_replace is much faster than preg_replace, and strtr is even faster than str_replace!  Save those crunch cycles... your enterprise applications will thank you.</p>
<div class="ad"><!--adsense#halfbanner--></div>
<p><strong>6.  Don't use functions inside a loop declaration.</strong> This isn't a PHP-specific tip, but you'll see it in a lot of code.</p>
<p>Bad:<br />
<code><br />
for ($i = 0; $i &lt; count($array); $i++) {<br />
//stuff<br />
}<br />
</code><br />
Good:<br />
<code><br />
$count = count($array);<br />
for($i = 0; $i &lt; $count; $i++) {<br />
//stuff<br />
}<br />
</code><br />
That should be pretty self-explanatory, but it's amazing how many people would rather save a line of code at the expense of performance.  If you use a function like count() inside a loop declaration, it's going to get executed at every iteration!  If your loop is large, you're using a lot of extra execution time.</p>
<p><strong>7.  Never rely on register_globals or magic quotes.</strong> <a href="http://www.php.net/manual/en/ini.core.php#ini.register-globals" target="_new">register_globals</a> and <a href="http://www.php.net/manual/en/security.magicquotes.php" target="_blank">magic quotes</a> are both old features of PHP that seemed like a good idea at the time (ten years ago), but in reality turned out to be not that great.  Older installations of PHP would have these features on by default, and they cause security holes, programming errors, and all sorts of bad practices, such as relying on user input to create variables.  Both these features are now deprecated, and everyone needs to stop using them.  If you are ever working on code that relies on these features, get it out of there as soon as you can!</p>
<p><strong>8.  Always initialize your variables. </strong> PHP will automatically create a variable if it hasn't been initialized, but it's not good practice to rely on this feature.  It makes for sloppy code, and in large functions or projects can become quite confusing if you have to track down where it's being created.  In addition, incrementing an uninitialized variable is much slower than if it was initialized.  It's just a good idea.</p>
<p><strong>9.  Document your code.</strong> You've heard it many times, but this can't be said enough.  I know places that won't hire people who don't document code.  I even <em>got my previous job</em> after an interview where the VP sat-in with the interviewer and I, where I had brought my laptop in and I was just scrolling through some code I had written for one of my sites.  He saw my documented functions and was impressed enough to ask me about my documenting habits.  A day later I had the job.</p>
<p>I know a lot of self-declared PHP gurus out there like to pretend that their code is so good that they don't have to spend time documenting it, and these people are full of <code>$largeAnimal</code> poop.  Learn docblock syntax, familiarize yourself with some PHP Documentation packages like <a href="http://manual.phpdoc.org/HTMLframesConverter/default/" target="_blank">phpDocumentor</a> or <a href="http://www.stack.nl/~dimitri/doxygen/" target="_blank">Doxygen</a>, and take the extra time to do it.  It's worth it.</p>
<p><strong>10.  Code to a standard.</strong> This is something that <strong>you should ask potential employers</strong> about during interviews.  Ask them what kind of coding standards they use... <a href="http://pear.php.net/manual/en/standards.php" target="_blank">PEAR</a>?  <a href="http://framework.zend.com/manual/en/coding-standard.html" target="_blank">Zend</a>?  In-house?  Mention that you code to a specific standard, whether it be your own, or one of the more prevalent ones out there.  The problem with loosely-typed languages like PHP is that without a proper coding standard, code tends to start looking like huge piles of garbage.  Stinky, disgusting garbage.  A basic set of rules that includes whitespace standards, brace matching, naming conventions, etc. is a must-have, must-follow for anyone who prides themselves on their code quality.</p>
<p>That being said, I hate all you space-indenters.  I mean, what the hell?  4 space characters as an indent?  That's exactly four times as much whitespace to parse as a tab.  More importantly, you can set your tab-stops to any value you want if you're using any text-editor more advanced than Notepad, so every developer can having something that they like the looks of.  Set it to 4 if you want, or 0 if you're a masochist.  I don't care, but you can't do that with spaces!  You're stuck with exactly the amount that Mr. Monkey Pants in cubicle 17 decided to put in.  So why are spaces so popular?  This "4-space indent" standard everyone uses is stupid!  Stop it!  Stop doing it!</p>
<p>... sorry, pet peeve.</p>
<p>Anyway, I hope these tips are helpful.  If you want to impress at a job interview, it's the little details that will get you noticed!  Maybe don't rant about your coding standards though.</p>
<div class="ad"><!--adsense#halfbanner--></div>
]]></content:encoded>
			<wfw:commentRss>http://www.phpvs.net/2008/06/04/ten-php-best-practices-tips-that-will-get-you-a-job/feed/</wfw:commentRss>
		<slash:comments>116</slash:comments>
		</item>
	</channel>
</rss>
