<?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>warpedvisions.org &#187; Php</title>
	<atom:link href="http://warpedvisions.org/topics/weblog/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://warpedvisions.org</link>
	<description>A technical tumblelog of links and articles on programming, design, and other geek interests</description>
	<lastBuildDate>Sat, 12 Jun 2010 21:20:01 +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>Optional arguments in PHP</title>
		<link>http://warpedvisions.org/2008/07/16/optional-arguments-in-php/</link>
		<comments>http://warpedvisions.org/2008/07/16/optional-arguments-in-php/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 03:33:49 +0000</pubDate>
		<dc:creator>mx</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://warpedvisions.org/?p=1929</guid>
		<description><![CDATA[Named, optional function arguments are a commonly used calling convention in Perl modules, used for constructors and heavily overloaded functions.  They&#8217;re usually implemented with hashes in Perl, and while somewhat more work to iterate in the library than fixed arguments, they clearly document the call for the caller (and tend to be far less <a rel="tag" target="_new" href="http://google.com/search?q=\%20&#38;btnI=">...</a>]]></description>
			<content:encoded><![CDATA[<p>Named, optional function arguments are a commonly used calling convention in Perl modules, used for constructors and heavily overloaded functions.  They&#8217;re usually implemented with hashes in Perl, and while somewhat more work to iterate in the library than fixed arguments, they clearly document the call for the caller (and tend to be far less fragile).</p>

<p>They&#8217;re also easy to implement in PHP. Here&#8217;s an example (based on some CodeIgniter code I had laying around):</p>

<pre class='prettyprint'><code>class bar extends Controller {

    protected $data = array();

    function foo($name, $extras = array(
            'desc' =&gt; 'default1', 
            'on' =&gt; false)) {
        $this-&gt;data = array_merge($this-&gt;data, $extras);
    }

}
</code></pre>

<ol>
<li>The optional parameters are defined as the array elements of the <code>$extra</code> default array parameter value.</li>
<li>The optional parameters are merged with the class data <code>$this-&gt;data</code>.</li>
</ol>

<p>Calling the function is simple:</p>

<pre class='prettyprint'><code>$b.foo('test', array('on' =&gt; true));
$b.foo('test 2' /* uses defaults */ );
$b.foo('test 3', array('desc' =&gt; 'Here is a description'));
</code></pre>

<p>The caller&#8217;s syntax isn&#8217;t quite as clean as Perl&#8217;s, but applying the defaults is cleaner (and merging the structures is about equivalent).  Despite the slightly more complex calling convention, it&#8217;s still useful for setup functions with multiple optional parameters.</p>
]]></content:encoded>
			<wfw:commentRss>http://warpedvisions.org/2008/07/16/optional-arguments-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Php++, and why Rails isn&#8217;t for me</title>
		<link>http://warpedvisions.org/2007/04/01/php-and-why-rails-isnt-for-me/</link>
		<comments>http://warpedvisions.org/2007/04/01/php-and-why-rails-isnt-for-me/#comments</comments>
		<pubDate>Sun, 01 Apr 2007 17:05:15 +0000</pubDate>
		<dc:creator>mx</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[idea]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://warpedvisions.org/2007/04/01/php-and-why-rails-isnt-for-me/</guid>
		<description><![CDATA[Here&#8217;s an idea that I haven&#8217;t had a chance to hack at yet: Php++.  Take the base language, clean it up using a simple pre-processor, and spit out regular Php.  Think of it like Rails, but inverted.  Instead of creating a few mini-languages to replace HTML/CSS/Javascript/SQL generation, find a way to string <a rel="tag" target="_new" href="http://google.com/search?q=\%20&#38;btnI=">...</a>]]></description>
			<content:encoded><![CDATA[<p><img style="float: right;" src="/images/fan.jpg" />Here&#8217;s an idea that I haven&#8217;t had a chance to hack at yet: Php++.  Take the base language, clean it up using a simple pre-processor, and spit out regular Php.  Think of it like Rails, but inverted.  Instead of creating a few mini-languages to replace HTML/CSS/Javascript/SQL generation, find a way to string these languages together in a cleaner way.  An <a href="http://en.wikipedia.org/wiki/Exoskeleton">exoskeleton</a> versus an <a href="http://en.wikipedia.org/wiki/Endoskeleton">endoskeleton</a>.</p>

<p>I don&#8217;t have anything against Ruby (it&#8217;s a cleaner, crisper Perl), and it&#8217;s not like I don&#8217;t appreciate what Rails tries to do.  What gets in my way is that Rails introduces new <a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language">domain-specific-languages</a> that aren&#8217;t as good as what they replace.</p>

<p>What Rail&#8217;s DSLs intend to do is great: automating database migrations, schema/code synchronization, managing project skeletons, Javascript integration, and more.  The problem is that these DSLs are specific to Rails, and represent non-portable knowledge.  You have to relearn how to do JOINs, UNIONs, and POSTS.  Not that the Rails way isn&#8217;t clever, but it&#8217;s a layer between a set of languages that are already really good.  My question is, what&#8217;s wrong with writing SQL, HTML, and JS?  I think the answer is that their intersection is messy, and you duplicate your application&#8217;s domain knowledge between each of the specific languages.</p>

<p>So Rails helps to enforce the <a href="http://en.wikipedia.org/wiki/One_Definition_Rule">One Definition Rule</a>, but it does so by inventing new mini-languages.  Rails does it well, which is forgivable, but I&#8217;m thinking that the same end is possible while still embracing SQL, HTML, and Javascript.  Anything you know about these languages should still apply, so anything you learn using the tool would be useful elsewhere.</p>

<p><strong>Enter Php++</strong></p>

<p>Add a small number of keywords similar to the QT C++ extensions that make it easy to generate sensible code from your SQL/HTML/JS.  For example:</p>

<pre class='prettyprint'><code>class student {
    public get address() = "SELECT * FROM Students WHERE id = '$sid'";
    public set address($street, $number, $etc) = "INSERT ...";

    // The display portions rely on get/set
    public edit show_edit_form() default(); 
    public edit show_inline_edit() default(ajax);
    public view display_full_address() default(); 

}
</code></pre>

<p>The extensions would also take care of the mess around escaping SQL, and could switch between various SQL backends without much trouble (mysql_*, Pear::DB, etc.).  And the Php++ processor would do the right thing, and would generate real, readable Php.</p>

<p>Now my only problem is finding the time to hack at it.</p>
]]></content:encoded>
			<wfw:commentRss>http://warpedvisions.org/2007/04/01/php-and-why-rails-isnt-for-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Php database interfaces</title>
		<link>http://warpedvisions.org/2006/10/29/php-database-interfaces/</link>
		<comments>http://warpedvisions.org/2006/10/29/php-database-interfaces/#comments</comments>
		<pubDate>Mon, 30 Oct 2006 00:20:07 +0000</pubDate>
		<dc:creator>mx</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Weblog]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://warpedvisions.org/2006/10/29/php-database-interfaces/</guid>
		<description><![CDATA[Let&#8217;s face it, the default Php MySQL interfaces are pretty weak. A few of the shortcomings:


No prepared statements
In lacking prepared statements, requires the use of escaping functions for any SQL containing POST/GET variables
Read functions overwrite duplicate column names in returned data when reading rows from joined tables
Has a very verbose API, with many long parameter <a rel="tag" target="_new" href="http://google.com/search?q=\%20&#38;btnI=">...</a>]]></description>
			<content:encoded><![CDATA[<p><a href="/images/bambo.jpg"><img src="/images/bambo_thumb.jpg" alt="bamboo" style="float:right; margin: .5em"/></a>Let&#8217;s face it, the default Php MySQL interfaces are pretty weak. A few of the shortcomings:</p>

<ul>
<li>No prepared statements</li>
<li>In lacking prepared statements, requires the use of escaping functions for any SQL containing POST/GET variables</li>
<li>Read functions overwrite duplicate column names in returned data when reading rows from joined tables</li>
<li>Has a very verbose API, with many long parameter lists</li>
<li>Is not class based, so everyone bakes their own abstraction</li>
</ul>

<p>Compared to the Perl DBI, for example, Php&#8217;s <code>mysql_</code> functions are absolutely horrid.  Luckily there are many great alternatives, as I discovered this weekend with a bit of research.</p>

<h4>The PDO</h4>

<p><a href="http://us2.php.net/pdo">Php&#8217;s PDO functions</a> are similar to <a href="http://dbi.perl.org/">Perl&#8217;s DBI</a>, sporting a reasonable API and supporting a number of databases.</p>

<h4>Php&#8217;s improved MySQL functions</h4>

<p>Php has an improved set of MySQL functions (the <a href="http://ca.php.net/mysqli"><code>mysqli_</code> functions</a>) that add both a simple class wrapper and a parallel, improved API.  The new API supports prepared statements, and has a cleaner interface layout.  The new APIs still munge the <code>fetch_assoc</code> functions (they still overwrite), but are otherwise a great improvement.</p>

<p>The improved MySQL functions are only available in Php 5, and I&#8217;ve read warnings that it&#8217;s a pain to run both the <code>msql_</code> and <code>mysqli_</code> functions from one Php installation.</p>

<h4>Pear&#8217;s MDB2</h4>

<p>The <a href="http://pear.php.net/package/MDB2">MDB2 package</a> from the <a href="http://pear.php.net">Pear repository</a> replaces <a href="http://pear.php.net/package/DB/redirected">their previous DB</a> package, and provides a similar interface to Perl&#8217;s DBI.  I find the interface a bit bigger than my needs present, but it is a very complete and mature abstraction.</p>

<h4>In the real world</h4>

<p>All of these libraries are available on most platforms and from most reasonable webhosts (like <a href="http://dreamhost.com/r.cgi?madmaxx/shared/comparison.html">Dreamhost</a>).</p>

<p>My first choice so far is the improved <code>mysqli_</code> class/functions, then the built-in PDO library.  I&#8217;ll spend a few weeks with them and then post a follow-up.</p>
]]></content:encoded>
			<wfw:commentRss>http://warpedvisions.org/2006/10/29/php-database-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Php, foot, mouth</title>
		<link>http://warpedvisions.org/2006/09/27/php-foot-mouth/</link>
		<comments>http://warpedvisions.org/2006/09/27/php-foot-mouth/#comments</comments>
		<pubDate>Thu, 28 Sep 2006 05:06:37 +0000</pubDate>
		<dc:creator>mx</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Weblog]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[suck]]></category>

		<guid isPermaLink="false">http://warpedvisions.org/2006/09/27/php-foot-mouth/</guid>
		<description><![CDATA[I found an interesting Php behaviour tonight.  The language allows you to call a member function as if it were a static function, something you can accidentally hide when calling callback functions:

// Oops, this calls a non-static member statically as sm::blocks
preg_replace_callback(
       "/(?&#60;=\n\n&#124;\r\n\r\n&#124;\r\r&#124;\A)(.*?)(?:\n\n&#124;\r\n\r\n&#124;\r\r&#124;\Z)/sm",
       <a rel="tag" target="_new" href="http://google.com/search?q=\%20&#38;btnI=">...</a>]]></description>
			<content:encoded><![CDATA[<p>I found an interesting <a rel="tag" target="_new" href="http://en.wikipedia.org/wiki/Php">Php</a> behaviour tonight.  The language allows you to call a member function as if it were a static function, something you can accidentally hide when calling callback functions:</p>

<pre class='prettyprint'><code>// Oops, this calls a non-static member statically as sm::blocks
preg_replace_callback(
       "/(?&lt;=\n\n|\r\n\r\n|\r\r|\A)(.*?)(?:\n\n|\r\n\r\n|\r\r|\Z)/sm",
       array("sm", "blocks"),
       $text);
}
</code></pre>

<p>What I really meant was:</p>

<pre class='prettyprint'><code>array($this, "blocks")
</code></pre>

<p>When you call a static function in a Php class, it has the side-effect of wiping out the class context.  Without the class context, you can&#8217;t make other calls to class members.  In my case above, Php allowed me to break the class context (calling a non-static as static) without warning, which caused a perfectly legitimate class member function fail:</p>

<pre class='prettyprint'><code>function text($m) {
    $this-&gt;inlines($m);
}
</code></pre>

<p>Which fails with: <code>Using $this when not in object context</code>.</p>

<p>Instead, Php should warn: <code>Calling a non-static member as static. This might not be what you meant to do</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://warpedvisions.org/2006/09/27/php-foot-mouth/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
