<?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>Jansch.nl &#187; anonymous classes</title>
	<atom:link href="http://www.jansch.nl/tag/anonymous-classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jansch.nl</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Sun, 31 Jul 2011 14:58:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Anonymous Classes</title>
		<link>http://www.jansch.nl/2005/11/19/anonymous-classes/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=anonymous-classes</link>
		<comments>http://www.jansch.nl/2005/11/19/anonymous-classes/#comments</comments>
		<pubDate>Sat, 19 Nov 2005 09:59:37 +0000</pubDate>
		<dc:creator>Ivo</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[anonymous classes]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php6]]></category>

		<guid isPermaLink="false">http://www.jansch.nl/?p=231</guid>
		<description><![CDATA[There have been some PHP6 wishlists in the recent past. I only have one wish: anonymous classes. Someone who uses Java will know what anonymous classes are, but for those who don't, I will explain with examples. Suppose we are dealing with buttons. We have a generic button class, that has an 'actionPerformed' method that [...]]]></description>
			<content:encoded><![CDATA[<p>There have been some PHP6 wishlists in the recent past. I only have one wish: anonymous classes.</p>
<p>Someone who uses Java will know what anonymous classes are, but for those who don't, I will explain with examples.</p>
<p>Suppose we are dealing with buttons. We have a generic button class, that has an 'actionPerformed' method that is executed when the button is clicked. The generic implementation just echo's something when the button is clicked:</p>
<pre class="php">&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Button
<span style="color: #66cc66;">&#123;</span>
  ....
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> actionPerformed<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;The button was clicked&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Button<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>Now we want to use a button in an application, with custom behaviour when clicked. Let's assume we want to display the name of the form the button is located in when clicked. Currently In PHP we would have to do it like this:</p>
<pre class="php">&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">class</span> CustomButton <span style="color: #000000; font-weight: bold;">extends</span> Button
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$m_form</span> = <span style="color: #000000; font-weight: bold;">NULL</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> CustomButton<span style="color: #66cc66;">&#40;</span>&amp;<span style="color: #0000ff;">$form</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_form</span> = &amp;<span style="color: #0000ff;">$form</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> actionPerformed<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;The custom button in the &quot;</span>.<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_form</span>-&gt;<span style="color: #006600;">getTitle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot; was clicked&quot;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> CustomButton<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$form</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>This always requires the creation of an extra class. If PHP would support anonymous classes though, the code could look like this:</p>
<pre class="php">&nbsp;
&nbsp;
<span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Button<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">&#123;</span>
                   <span style="color: #000000; font-weight: bold;">function</span> actionPerformed<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">&#123;</span>
                     <a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;The custom button in the &quot;</span>.<span style="color: #0000ff;">$form</span>-&gt;<span style="color: #006600;">getTitle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot; was clicked&quot;</span>;
                   <span style="color: #66cc66;">&#125;</span>
                 <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>What you basically do is create an instance of the Button class with a custom actionPerformed method appended to it. In essence this internally would create a nameless (hence 'anonymous') class that extends Button.</p>
<p>The advantage is that there's no need to create a new class for each button. Furthermore, this construct creates an 'inner anonymous class' that is in the same scope as the surrounding code, so the $form variable is directly accessible from the methods of the anonymous class.</p>
<p>This is also particularly useful when dealing with things like Listeners, which are used heavily in Java in conjunction with all sorts of design patterns (MVC, Observer, ActiveRecord etc). Example:</p>
<pre class="php">&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">// Add a listener to perform some actions whenever a record is added.</span>
<span style="color: #0000ff;">$rowset</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RecordListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> recordAdded<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    refreshRecordCache<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    reloadPage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>I hope PHP6 will feature this. In my opinion, it can be a very powerful OO technique.</p>
<p>I've tried to emulate this in PHP4/5 but so far have not found a solution that is easier than just creating new classes. Ideas are more than welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jansch.nl/2005/11/19/anonymous-classes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

