<?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>Flexmaniak.pl &#187; magic function</title>
	<atom:link href="http://blog.flexmaniak.pl/tag/magic-function/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.flexmaniak.pl</link>
	<description>Flex, Papervision3D, PHP, Symfony</description>
	<lastBuildDate>Sun, 02 Oct 2011 08:36:28 +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>Overriding setter/getter in Symfony</title>
		<link>http://blog.flexmaniak.pl/flex-and-php/overriding-settergetter-in-symfony</link>
		<comments>http://blog.flexmaniak.pl/flex-and-php/overriding-settergetter-in-symfony#comments</comments>
		<pubDate>Fri, 02 Jul 2010 10:23:11 +0000</pubDate>
		<dc:creator>damian</dc:creator>
				<category><![CDATA[Flex and PHP]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[getter]]></category>
		<category><![CDATA[magic function]]></category>
		<category><![CDATA[setter]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[_set]]></category>

		<guid isPermaLink="false">http://blog.flexmaniak.pl/?p=851</guid>
		<description><![CDATA[Overriding methods should not be a problem, but it can make you confused if you don&#8217;t know how magic functions are used in Symfony 1.4 models. In this short article I will explain why usual overriding does not work in Symfony models and how to override methods handled by magic functions. Suppose, we have a [...]]]></description>
			<content:encoded><![CDATA[<p>Overriding methods should not be a problem, but it can make you confused if you don&#8217;t know how magic functions are used in Symfony 1.4 models.</p>
<p>In this short article I will explain why usual overriding does not work in Symfony models and how to override methods handled by magic functions.</p>
<p><span id="more-851"></span></p>
<p>Suppose, we have a base model class <strong>BaseTopic</strong>:</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">abstract <span style="color: #000000; font-weight: bold;">class</span> BaseTopic <span style="color: #000000; font-weight: bold;">extends</span> sfDoctrineRecord<br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setTableDefinition<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTableName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'topic'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
<span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">'notnull'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
<span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'updates_count'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'integer'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><br />
<span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'integer'</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>and our class:</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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Topic <span style="color: #000000; font-weight: bold;">extends</span> BaseTopic<br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now we want to update <strong>updates_count</strong> field each time we modify <strong>name</strong> field.</p>
<p>First guess is to do the following:</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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Topic <span style="color: #000000; font-weight: bold;">extends</span> BaseTopic<br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">updates_count</span><span style="color: #339933;">++;</span><br />
<br />
<span style="color: #b1b100;">return</span> parent<span style="color: #339933;">::</span><span style="color: #004000;">setName</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>However it will not work unless you have declared method <strong>setName</strong> in <strong>BaseTopic</strong> literally (magical <strong>__call</strong> does not count).</p>
<p>Upper method will do the following:</p>
<ol>
<li>Increment <strong>updates_count</strong> field</li>
<li>Call <strong>setName </strong>method from <strong>BaseTopic</strong> (which does not exist literally) so it will look for this method in parents</li>
<li>Because there is no <strong>setName</strong> in any parent class, it will go to <strong>Doctrine_Record</strong> and call <strong>__call</strong> method which will find <strong>setName </strong>method in <strong>Topic</strong> class</li>
<li>Call <strong>setName </strong>from <strong>Topic </strong>class again</li>
</ol>
<p>To solve this problem you have to use <strong>_set</strong> function from <strong>Doctrine_Record</strong> class:</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: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Topic <span style="color: #000000; font-weight: bold;">extends</span> BaseTopic<br />
<span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">updates_count</span><span style="color: #339933;">++;</span><br />
<br />
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_set<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Remember to use function <strong>_set()</strong>, not <strong>set().</strong></p>
<p>Of course there is no problem if you have defined <strong>setName</strong> method in any of <strong>Topic</strong>&#8216;s parent classes literally.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexmaniak.pl/flex-and-php/overriding-settergetter-in-symfony/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

