<?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>A Collection of Thoughts &#187; Static method calls</title>
	<atom:link href="http://www.paul-norman.co.uk/tag/static-method-calls/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paul-norman.co.uk</link>
	<description>PHP, MySQL, Coding, Javascript, HTML, CSS</description>
	<lastBuildDate>Mon, 04 Feb 2013 17:12:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Dynamically Named Static Method Calls</title>
		<link>http://www.paul-norman.co.uk/2009/06/dynamicly-named-static-method-calls/</link>
		<comments>http://www.paul-norman.co.uk/2009/06/dynamicly-named-static-method-calls/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 22:34:08 +0000</pubDate>
		<dc:creator>Paul Norman</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Static method calls]]></category>

		<guid isPermaLink="false">http://www.paul-norman.co.uk/2009/06/dynamicly-named-static-method-calls/</guid>
		<description><![CDATA[<p>Something that is being added in PHP 5.3.0 but is sorely missed in current versions is the ability to call a static class method from variables. It is something that is occasionally very useful (e.g. you have a validation class triggering other regional classes) and is already possible with standard class calls. Thankfully it's very easy to simulate with eval...</p>]]></description>
			<content:encoded><![CDATA[<p>Something that is being added in PHP 5.3.0 but is sorely missed in current versions is the ability to call a static class method from variables, for example:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'classname'</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'methodname'</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$vars</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'whatever'</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Only possible in PHP 5.3.0 and above</span><br />
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class</span><span style="color: #339933;">::</span><span style="color: #000088;">$method</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vars</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>

<p>It is something that is occasionally very useful (e.g. you have a validation class triggering other regional classes) and is already possible with standard class calls. Thankfully it&#8217;s very easy to simulate with eval:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> static_call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arguments</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arguments</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$arg_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$count</span>&nbsp; &nbsp; &nbsp; <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arguments</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$arg_string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$arg_string</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'$arguments['</span><span style="color: #339933;">.</span><span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">']'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$arg_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'$arguments'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #990000;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$return = '</span><span style="color: #339933;">.</span><span style="color: #000088;">$class</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'::'</span><span style="color: #339933;">.</span><span style="color: #000088;">$method</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'('</span><span style="color: #339933;">.</span><span style="color: #000088;">$arg_string</span><span style="color: #339933;">.</span><span style="color: #0000ff;">');'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>

<p>Using it is very simple:</p>

<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'classname'</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'methodname'</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$vars</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'whatever'</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> static_call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vars</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>

<p>And will handle a single variable or an array of the required variables in order. There&#8217;s not much else to say apart from use it wisely!</p>]]></content:encoded>
			<wfw:commentRss>http://www.paul-norman.co.uk/2009/06/dynamicly-named-static-method-calls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
