<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>giant dancing chipmunk &#187; php</title>
	<atom:link href="http://nessence.net/category/code/php-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://nessence.net</link>
	<description>Do you remember when people used browsers?</description>
	<lastBuildDate>Fri, 18 Nov 2011 21:50:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='nessence.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>giant dancing chipmunk &#187; php</title>
		<link>http://nessence.net</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://nessence.net/osd.xml" title="giant dancing chipmunk" />
	<atom:link rel='hub' href='http://nessence.net/?pushpress=hub'/>
		<item>
		<title>PHP4 + PHP5 compatible overloading</title>
		<link>http://nessence.net/2008/05/01/php4-php5-compatible-overloading/</link>
		<comments>http://nessence.net/2008/05/01/php4-php5-compatible-overloading/#comments</comments>
		<pubDate>Thu, 01 May 2008 10:23:37 +0000</pubDate>
		<dc:creator>Alex Leverington</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[oop overloading polymorphism php4 php5]]></category>

		<guid isPermaLink="false">http://nessence.wordpress.com/?p=23</guid>
		<description><![CDATA[While I don&#8217;t necessarily enjoy PHP, it&#8217;s not such a terrible language. At least for version 5. Lately I&#8217;ve been working on a project called &#8216;migrations&#8217; which is essentially a PHP utility to generate / track / apply changes to a database in a similar fashion to the migrations system in Ruby on Rails. Of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nessence.net&amp;blog=7091&amp;post=23&amp;subd=nessence&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While I don&#8217;t necessarily enjoy PHP, it&#8217;s not such a terrible language. At least for version 5. Lately I&#8217;ve been working on a project called &#8216;migrations&#8217; which is essentially a PHP utility to generate / track / apply changes to a database in a similar fashion to the migrations system in Ruby on Rails. Of course, it&#8217;s got more magic than migrations (only because we know we&#8217;re going to use subversion) and it&#8217;s got to work with PHP4.</p>
<p>Let&#8217;s get back to the subject though. When writing any type of class that needs to be polymorphic or &#8216;dynamic&#8217; in a sense that method calls can be made when a pre-defined method doesn&#8217;t exist, then &#8216;overloading&#8217; is the magic PHP provides to satisfy your needs. As a note, polymorphism is simply the ability of a method to take varying parameters or by which a different method will be executed based on the types and/or quantity of methods required. For example, you could have a different method defined for function($array) than function($string). PHP doesn&#8217;t care about types and so doesn&#8217;t support polymorphism <em>per se</em> but by using overloading you can essentially accomplish the same thing. In my case however, I&#8217;m simply wanting to allow execution of non-existent methods against on object. The purpose is that I want to be able to log each method call and it&#8217;s result in order to track what&#8217;s going on. You&#8217;ll quickly see what I mean in the examples.</p>
<p>PHP5 Basic Example:</p>
<pre>class dynamicClass {
    function __call($method, $arguments) {
        trigger_error('Call to undefined method ' . __CLASS__ . '::' . $method . '()', E_USER_ERROR);
    }
}</pre>
<p>Now have a class in PHP5 with overloading enabled. Without trigger_error, PHP will always think that you&#8217;re calling a valid method! In PHP5, there is no way to tell the interpreter the called method is invalid and you are responsible for triggering an error.</p>
<p>PHP4 Basic Example:</p>
<pre>class dynamicClass {
    function __call($method, $arguments, &amp;$return) {
        if($method == __CLASS__)
            return true;

        trigger_error('Call to undefined method ' . __CLASS__ . '::' . $method . '()', E_USER_ERROR);
    }
}</pre>
<p> <br />
In PHP4, overloading was experimental for awhile and is now not well documented because it changed so much in PHP5. The first two lines of the __call method exist because the class doesn&#8217;t have a constructor defined but we don&#8217;t want to return false or null &#8211; if we do &#8211; PHP will throw a warning that we made a call to an undefined method, as it tries to call the non-existent constructor. Without overloading PHP4 ignores non-existent constructors, however, since we&#8217;ve overloaded and the constructor doesn&#8217;t already exist &#8211; PHP4 hits __call. This is different from PHP5 wherein PHP5 will not try to make an overload call for any constructor. This is likely because in PHP5 constructors or pseudo-static methods (they are not called within the context of an object even though the constructor has access to the object &#8211; this is why you get to use the $this variable even in a static context &#8211; if you couldn&#8217;t PHP5 constructors wouldn&#8217;t work ;) )</p>
<p>So between these two examples we can see some obvious limitations.</p>
<ol>
<li>don&#8217;t use overloading for anything regarding constructors</li>
<li>php4 uses &amp;$return declaration for return, php5 uses return()</li>
<li>php4 is the exception, not php5; php5 autoload will work just fine</li>
<li>in php4, you&#8217;re class is limited to being declared from within an eval() block</li>
</ol>
<p>This makes for complications if you want to create a class with overloading which will work both in PHP4 and PHP5. There is the safe way, not-so-safe way, and the somewhat-safe way that&#8217;s a little slower! First, I&#8217;ll show you the not-so-safe way which I&#8217;ll be using simply because this is a utility and not something I expect someone to modify and <em>secondly</em> because integration tests tell me if/when something breaks. If you don&#8217;t write tests I would highly recommend the &#8216;safer&#8217; way.</p>
<p>File &#8211; dynamicOverloaded.class.php:</p>
<pre>class dynamicOverloaded {
    function __call($method, $arguments /*PHP4, &amp;$return*/) {
        /*PHP4if($method == __CLASS__) return true;*/

        // You're overloading logic

        $return = $resultFromYourMagic;
        return ($return ? $return : true);
    }
}</pre>
<p>You maybe asking yourself now &#8220;wtf&#8221; or &#8220;how is that supposed to work?&#8221; and you would be right &#8211; it&#8217;s not going to work yet. In order to make this work, you can&#8217;t require() or include(), or otherwise execute this file. Unless of course, the version of PHP is &gt;= 5, in either case, the following code will do the trick.</p>
<p>The not-so-safe way:</p>
<pre>if(substr(PHP_VERSION, 0, strrpos(PHP_VERSION, '.')) &lt; 5)
    eval('?&gt;' . preg_replace('|/\*PHP4(.*?)\*/|', '\\1', file_get_contents('path/to/dynamicOverloaded.class.php')) );
else
    require_once('dynamicOverloaded.class.php');</pre>
<p>As with any php script, you have to include it. But in this case we&#8217;re eval()&#8217;ing the contents of the file instead of including it when using PHP4. Before eval we&#8217;re stripping /*PHP4 */ comments and leaving the code within it intact. The result? It&#8217;s like pre-compile macros for PHP. This is called the not-so-safe way primarily due to all of the limitations and bugs from eval().</p>
<p>But what is the safe method, and what if I&#8217;m doing something eval() doesn&#8217;t like?</p>
<p>In this case, you want to change your code to check for the version of PHP &#8211; create one class called yourClass.class4.php and yourclass.class.php; if using PHP4&#8230; you get the idea. Make your require() statements dynamic. The class4.php script will have the same code but you&#8217;ll remove the /*PHP4 */ comments before you save the file (leave the code between the comment markers in place). As you can see the real trick is to have your classes declared in separate files. The problem with this and the reason it&#8217;s not called &#8220;safe&#8221; is because you&#8217;re going to have to keep your changes to the class synchronized across both files at all times! While annoying, you can be happy know that you&#8217;re not eval()&#8217;ing code or executing temporary files.</p>
<p>But what if I don&#8217;t want to have to keep changes synchronized between class files and I don&#8217;t mind it being a <strong>tiny bit</strong> slow?</p>
<p>Before requiring the class file so you&#8217;ll want to name the base class yourClass.classX.php with the /*PHP4 */ blocks of code in it &#8211; before performing require() you literally &#8216;compile&#8217; the class by running the preg_replace and saving the result to a temp file on disk; then require() the tmp file and delete the tmp file. Now obviously there can be some risks associated with executing a file which is written to disk by the executing process (esp if it&#8217;s a web server) but you can do a few things such as making the file name random to mitigate risks. Whatever you do, be sure the temporary location is relatively safe (consult chmod man page) and that above all the generated script is deleted after it&#8217;s executed.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nessence.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nessence.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nessence.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nessence.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nessence.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nessence.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nessence.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nessence.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nessence.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nessence.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nessence.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nessence.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nessence.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nessence.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nessence.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nessence.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nessence.net&amp;blog=7091&amp;post=23&amp;subd=nessence&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nessence.net/2008/05/01/php4-php5-compatible-overloading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a73a1ab6b15c0f17db9cb7dc7add9a2e?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=X" medium="image">
			<media:title type="html">nessence</media:title>
		</media:content>
	</item>
		<item>
		<title>Coding like it&#8217;s 1999</title>
		<link>http://nessence.net/2008/04/25/coding-like-its-1999/</link>
		<comments>http://nessence.net/2008/04/25/coding-like-its-1999/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 04:25:18 +0000</pubDate>
		<dc:creator>Alex Leverington</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web applications]]></category>
		<category><![CDATA[!microsoft]]></category>
		<category><![CDATA[bitter soap]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[perl died]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[top ten]]></category>

		<guid isPermaLink="false">http://nessence.wordpress.com/?p=17</guid>
		<description><![CDATA[Here is my top ten list of &#8220;How to code like it&#8217;s 1999&#8243;: 1. using PHP &#60; 5.3 (4 was beta in 1999) 2. making use of perl for *anything* (see #4) 3. template engines (scope and variable interpolation exist for a reason) 4. Perl6 (active 1999 mailing list) 5. Java Web Applets 6. SELECT [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nessence.net&amp;blog=7091&amp;post=17&amp;subd=nessence&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is my top ten list of &#8220;How to code like it&#8217;s 1999&#8243;:</p>
<p>1. using PHP &lt; 5.3 (4 was beta in 1999)<br />
2. making use of perl for *anything* (see #4)<br />
3. template engines (scope and variable interpolation exist for a reason)<br />
4. Perl6 (active 1999 mailing list)<br />
5. Java Web Applets<br />
6. SELECT * FROM (seriously, get ORM, select only what you need, or quit your day job.)<br />
7. lisp (like a hero from a classic book, tragic)<br />
8. non-functional redirect pages (hello js or location header; good-bye bad knocking off a lame phpbb feature)<br />
9. ActiveX plug-ins (who developers sites with IE, anyways?)<br />
10. SOAP (all your interoperability are belong to SOAP)</p>
<p>In short, if your core application for which your business and revenue suffers any of the above atrocities, step back for a minute and ask yourself if you <strong>really</strong> know any better? If you don&#8217;t, hire someone who does &#8211; FAST!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nessence.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nessence.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nessence.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nessence.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nessence.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nessence.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/nessence.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/nessence.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/nessence.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/nessence.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nessence.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nessence.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nessence.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nessence.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nessence.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nessence.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nessence.net&amp;blog=7091&amp;post=17&amp;subd=nessence&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://nessence.net/2008/04/25/coding-like-its-1999/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a73a1ab6b15c0f17db9cb7dc7add9a2e?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=X" medium="image">
			<media:title type="html">nessence</media:title>
		</media:content>
	</item>
	</channel>
</rss>
