<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Dynamic global functions in PHP</title>
	<atom:link href="http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/</link>
	<description>News, opinion, and fresh thinking for web developers and designers. The official podcast of sitepoint.com.</description>
	<pubDate>Tue, 02 Dec 2008 04:15:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: ballyhoos</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-840155</link>
		<dc:creator>ballyhoos</dc:creator>
		<pubDate>Thu, 27 Nov 2008 11:50:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-840155</guid>
		<description>&lt;blockquote&gt;I think using global functions is a bad practice anyway. Even with the described improvement it may end up with code that is hard to maintain.&lt;/blockquote&gt;

How can you say that...that's the stupidest thing I've heard.  I don't see how you think that using global functions would create code that is hard to maintain?  Talking from experience with creating a php framework it allows for extremely quick reference and using global functions for like url replacing within href tags and is extremely useful.  

All my global functions start with an underscore thus differentiating from php defined global functions like __autload etc... maybe that could solve the conflict?</description>
		<content:encoded><![CDATA[<blockquote><p>I think using global functions is a bad practice anyway. Even with the described improvement it may end up with code that is hard to maintain.</p></blockquote>
<p>How can you say that&#8230;that&#8217;s the stupidest thing I&#8217;ve heard.  I don&#8217;t see how you think that using global functions would create code that is hard to maintain?  Talking from experience with creating a php framework it allows for extremely quick reference and using global functions for like url replacing within href tags and is extremely useful.  </p>
<p>All my global functions start with an underscore thus differentiating from php defined global functions like __autload etc&#8230; maybe that could solve the conflict?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Fritz</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-463501</link>
		<dc:creator>Fritz</dc:creator>
		<pubDate>Wed, 14 Nov 2007 00:49:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-463501</guid>
		<description>&lt;code&gt;
inspires a static class e
e::s($arg) // htmlspecialchars
e::d($arg) // date format
e::n($arg) // numeric format
e::U($arg) // uppercase

...etc, you get the picture
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<code>
inspires a static class e
e::s($arg) // htmlspecialchars
e::d($arg) // date format
e::n($arg) // numeric format
e::U($arg) // uppercase

...etc, you get the picture
</code>]]></content:encoded>
	</item>
	<item>
		<title>By: micmath</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-439512</link>
		<dc:creator>micmath</dc:creator>
		<pubDate>Tue, 06 Nov 2007 09:33:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-439512</guid>
		<description>Can I suggest? &lt;code&gt;escho()&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Can I suggest? <code>escho()</code></p>]]></content:encoded>
	</item>
	<item>
		<title>By: michel</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-429980</link>
		<dc:creator>michel</dc:creator>
		<pubDate>Mon, 29 Oct 2007 14:19:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-429980</guid>
		<description>&lt;blockquote&gt;stream wrappers are just a fancy way of doing file_get_contents() and eval() it. To use that, you would have to parse the content, in which case, I think you have moved away from the PHP-as-template language paradigm.&lt;/blockquote&gt;
I'd like to nuance that: yes, you're moving away from 'out-of-the-box' PHP templating by depending on a parsing layer. You can still use PHP *syntax*, however. I think shortcuts can be very convenient in templates, especially if you have to produce a high volume of template code. To stick with the example, why type &lt;code&gt;&#60;?php echo (expression) ?&#62;&lt;/code&gt; when you can support &lt;code&gt;&#60;?=(expression)?&#62;&lt;/code&gt; with minimal effort?</description>
		<content:encoded><![CDATA[<blockquote><p>stream wrappers are just a fancy way of doing file_get_contents() and eval() it. To use that, you would have to parse the content, in which case, I think you have moved away from the PHP-as-template language paradigm.</p></blockquote>
<p>I&#8217;d like to nuance that: yes, you&#8217;re moving away from &#8216;out-of-the-box&#8217; PHP templating by depending on a parsing layer. You can still use PHP *syntax*, however. I think shortcuts can be very convenient in templates, especially if you have to produce a high volume of template code. To stick with the example, why type <code>&lt;?php echo (expression) ?&gt;</code> when you can support <code>&lt;?=(expression)?&gt;</code> with minimal effort?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Avi</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-426529</link>
		<dc:creator>Avi</dc:creator>
		<pubDate>Fri, 26 Oct 2007 14:20:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-426529</guid>
		<description>&lt;blockquote&gt;The view, chosen by the controller, would know how to present that given data be it XML, JSON et al.&lt;/blockquote&gt;
If we're gonna deal with this in an object oriented fashion (because you're treating the view as an entity), why not encapsulate the view in an object which has a method for escaping html. This way we could avoid having global functions in the first place</description>
		<content:encoded><![CDATA[<blockquote><p>The view, chosen by the controller, would know how to present that given data be it XML, JSON et al.</p></blockquote>
<p>If we&#8217;re gonna deal with this in an object oriented fashion (because you&#8217;re treating the view as an entity), why not encapsulate the view in an object which has a method for escaping html. This way we could avoid having global functions in the first place</p>]]></content:encoded>
	</item>
	<item>
		<title>By: kyberfabrikken</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-425455</link>
		<dc:creator>kyberfabrikken</dc:creator>
		<pubDate>Thu, 25 Oct 2007 13:18:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-425455</guid>
		<description>&lt;blockquote&gt;
Even simpler, create a prefixed function and call it by name stored in a var...
&lt;/blockquote&gt;
Yes, that's an even simpler way of implementing it. There are two reasons why I wanted to hide the callback behind a regular function.

The first is, that people are generally uncomfortable with callbacks. Even if it &lt;em&gt;is&lt;/em&gt; a valid syntax, it's bound to create confusion. As templates are otherwise fairly simple code, it's something that inexperienced programmers often deal with, and they wouldn't have an easy time with a callback syntax.

The other problem is, that you can only use regular functions. If you want to create a callback to an object method, you need to use the convoluted &lt;code&gt;call_user_func()&lt;/code&gt;.

&lt;blockquote&gt;
If you’re capturing template output within object context, a stream wrapper to rewrite PHP could be a feasible option.
&lt;/blockquote&gt;

stream wrappers are just a fancy way of doing &lt;code&gt;file_get_contents()&lt;/code&gt; and &lt;code&gt;eval()&lt;/code&gt; it. To use that, you would have to parse the content, in which case, I think you have moved away from the PHP-as-template language paradigm.</description>
		<content:encoded><![CDATA[<blockquote><p>
Even simpler, create a prefixed function and call it by name stored in a var&#8230;
</p></blockquote>
<p>Yes, that&#8217;s an even simpler way of implementing it. There are two reasons why I wanted to hide the callback behind a regular function.</p>
<p>The first is, that people are generally uncomfortable with callbacks. Even if it <em>is</em> a valid syntax, it&#8217;s bound to create confusion. As templates are otherwise fairly simple code, it&#8217;s something that inexperienced programmers often deal with, and they wouldn&#8217;t have an easy time with a callback syntax.</p>
<p>The other problem is, that you can only use regular functions. If you want to create a callback to an object method, you need to use the convoluted <code>call_user_func()</code>.</p>
<blockquote><p>
If you’re capturing template output within object context, a stream wrapper to rewrite PHP could be a feasible option.
</p></blockquote>
<p>stream wrappers are just a fancy way of doing <code>file_get_contents()</code> and <code>eval()</code> it. To use that, you would have to parse the content, in which case, I think you have moved away from the PHP-as-template language paradigm.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: michel</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-425327</link>
		<dc:creator>michel</dc:creator>
		<pubDate>Thu, 25 Oct 2007 11:48:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-425327</guid>
		<description>Oops, example nearly stripped: &lt;code&gt;&lt;/code&gt; becomes &lt;code&gt;e('foo')?&#62;&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Oops, example nearly stripped: <code></code> becomes <code>e('foo')?&gt;</code></p>]]></content:encoded>
	</item>
	<item>
		<title>By: michel</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-425320</link>
		<dc:creator>michel</dc:creator>
		<pubDate>Thu, 25 Oct 2007 11:44:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-425320</guid>
		<description>If you're capturing template output within object context, a stream wrapper to rewrite PHP could be a feasible option.
The stream wrapper interface makes it very easy to rewrite shorthand to verbose statements. For example,  becomes e('foo')?&#62;. All the advantages of encapsulation without the disadvantages of global scope.
I may be stating the obvious of course.</description>
		<content:encoded><![CDATA[<p>If you&#8217;re capturing template output within object context, a stream wrapper to rewrite PHP could be a feasible option.<br />
The stream wrapper interface makes it very easy to rewrite shorthand to verbose statements. For example,  becomes e(&#8217;foo&#8217;)?&gt;. All the advantages of encapsulation without the disadvantages of global scope.<br />
I may be stating the obvious of course.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Dr Livingston</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-423614</link>
		<dc:creator>Dr Livingston</dc:creator>
		<pubDate>Tue, 23 Oct 2007 20:56:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-423614</guid>
		<description>&#62; In otherwords, I see nothing wrong with having the controller 
&#62; format data as well…

But that isn't the role that the controller has though; the responsibility that you are talking about belongs in the model first, to return the data in correct package, and the view, to present it as required.

The view, chosen by the controller, would know how to present that given data be it XML, JSON et al.</description>
		<content:encoded><![CDATA[<p>&gt; In otherwords, I see nothing wrong with having the controller<br />
&gt; format data as well…</p>
<p>But that isn&#8217;t the role that the controller has though; the responsibility that you are talking about belongs in the model first, to return the data in correct package, and the view, to present it as required.</p>
<p>The view, chosen by the controller, would know how to present that given data be it XML, JSON et al.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: mrclay</title>
		<link>http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-423168</link>
		<dc:creator>mrclay</dc:creator>
		<pubDate>Tue, 23 Oct 2007 12:33:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/10/21/dynamic-global-functions-in-php/#comment-423168</guid>
		<description>Even simpler, create a prefixed function and call it by name stored in a var:
&lt;pre&gt;&lt;code class="php"&gt;function MyPrefix_e($str) {
    echo htmlspecialchars($str);
}
$e = 'MyPrefix_e';

// in template PHP block
$e($escapeMe);&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Even simpler, create a prefixed function and call it by name stored in a var:</p>
<pre><code class="php">function MyPrefix_e($str) {
    echo htmlspecialchars($str);
}
$e = 'MyPrefix_e';

// in template PHP block
$e($escapeMe);</code></pre>]]></content:encoded>
	</item>
</channel>
</rss>
