<?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>epiphantastic &#187; PHP</title>
	<atom:link href="http://www.epiphantastic.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.epiphantastic.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 13 Apr 2011 19:20:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Dynamic PHP</title>
		<link>http://www.epiphantastic.com/2006/12/12/dynamic-php/</link>
		<comments>http://www.epiphantastic.com/2006/12/12/dynamic-php/#comments</comments>
		<pubDate>Tue, 12 Dec 2006 05:18:25 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.epiphantastic.com/?p=11</guid>
		<description><![CDATA[When you&#8217;re trying to build more complex classes/applications, the need for doing things dynamically often arises. I figured I&#8217;d compile a list of ways to do dynamic PHP for those starting out with PHP. Please note that the list was elaborated with PHP 5 in mind. Some items may work with previous versions, but you&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>
	When you&#8217;re trying to build more complex classes/applications, the need for doing things dynamically often arises. I figured I&#8217;d compile a list of ways to do dynamic PHP for those starting out with PHP. Please note that the list was elaborated with PHP 5 in mind. Some items may work with previous versions, but you&#8217;ll have to test it out for that.
</p>
<h1>Variable Variables</h1>
<p class="pagetext">
	Suppose you need to output a variable but you don&#8217;t know in advance which variable it. Take a look at this code:<br />
	<textarea name="code" class="php" style="width:100%" rows="8"><br />
$hello = &#8220;Hello there!&#8221;;<br />
$hey = &#8220;Hey you!&#8221;;<br />
$greet = &#8220;hello&#8221;;</p>
<p>if ($greet == &#8220;hello&#8221;)<br />
	echo $hello;<br />
else if ($greet == &#8220;hey&#8221;)<br />
	echo $hey;<br />
	</textarea><br />
	Using variable variables, you can rewrite the above as follows:<br />
	<textarea name="code" class="php" style="width:100%" rows="5"><br />
$hello = &#8220;Hello there!&#8221;;<br />
$hey = &#8220;Hey you!&#8221;;<br />
$greet = &#8220;hello&#8221;;</p>
<p>echo $$greet;</textarea><br />
	Because <em>$greet == &#8220;hello&#8221;</em>, the end of the script runs as if it were echo <em>$hello</em>. <a href="http://www.php.net/manual/en/language.variables.variable.php" target="_blank">More details on variable variables here</a>.
</p>
<h1>Dynamic Functions/Objects</h1>
<p class="pagetext">
	The same principle described above can also be applied to functions, member variables, and functions, and object instantiation. Check this out:<br />
	<textarea name="code" class="php" style="width:100%" rows="6"><br />
// Dynamic functions<br />
function test() {<br />
	echo &#8220;This function can be called dynamically!&#8221;;<br />
}<br />
$function_name = &#8220;test&#8221;;<br />
$function_name();</p>
<p>// Dynamic member variables, functions, and objects<br />
class TestClass<br />
{<br />
	public $testvar = &#8220;Property of an object.&#8221;;</p>
<p>	public function testfunc($text) {<br />
		echo $text;<br />
	}<br />
}</p>
<p>$dynobj = &#8220;TestClass&#8221;;<br />
$dynvar = &#8220;testvar&#8221;;<br />
$dynfunc = &#8220;testfunc&#8221;;</p>
<p>// Dynamic object instantiation<br />
$obj = new $dynobj();</p>
<p>// Dynamic member variable access<br />
echo $obj->$dynvar;</p>
<p>// Dynamic member function access<br />
$obj->$dynfunc(&#8220;Wow, this works!&#8221;);</textarea><br />
	Isn&#8217;t that exciting? Gotta love PHP for making it so easy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.epiphantastic.com/2006/12/12/dynamic-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

