<?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>Internet Strategy Guide &#187; magic __call</title>
	<atom:link href="http://phpprotip.com/tag/magic-__call/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpprotip.com</link>
	<description>Together we can defeat the internet</description>
	<lastBuildDate>Tue, 08 Nov 2011 06:19:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Magic __call() vs. multiple setters</title>
		<link>http://phpprotip.com/2009/03/magic-__call-vs-multiple-setters/</link>
		<comments>http://phpprotip.com/2009/03/magic-__call-vs-multiple-setters/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 15:44:46 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[debatable]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rants]]></category>
		<category><![CDATA[debate]]></category>
		<category><![CDATA[i'm doing it wrong]]></category>
		<category><![CDATA[magic __call]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[opinion]]></category>
		<category><![CDATA[php noob]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=121</guid>
		<description><![CDATA[I recently came across a situation where a few of my setProperty() methods where exactly the same. Since I've gotten into improving my coding, this bugged me because it was repetitious code. Example: public function setFoo($foo=null) { if (is_string($foo)&#124;&#124;is_numeric($foo)) { $this->foo=$foo; return $this; } throw new Custom_Example_Exception("Please provide a proper string or numeric",Custom_Example_Exception::INVALID_TYPE); } public [...]]]></description>
			<content:encoded><![CDATA[<p>I recently came across a situation where a few of my setProperty() methods where exactly the same. Since I've gotten into improving my coding, this bugged me because it was repetitious code.<br />
Example:</p>
<pre>
public function setFoo($foo=null) {
	if (is_string($foo)||is_numeric($foo)) {
		$this->foo=$foo;
		return $this;
	}
	throw new Custom_Example_Exception("Please provide a proper string or numeric",Custom_Example_Exception::INVALID_TYPE);
}

public function setBar($bar=null) {
	if (is_string($bar)||is_numeric($bar)) {
		$this->bar=$bar;
		return $this;
	}
	throw new Custom_Example_Exception("Please provide a proper string or numeric",Custom_Example_Exception::INVALID_TYPE);
}
</pre>
<p>Notice a pattern there? Me too. If all my setters were like this, I could do </p>
<pre>
public function setProperty($property=null) {
	if (is_string($property)||is_numeric($property)) {
		$this->{$property}=$property;
		return $this;
	}
	throw new Custom_Example_Exception("Please provide a proper string or numeric",Custom_Example_Exception::INVALID_TYPE);
}
</pre>
<p>with appropriate traps in the __call() and maybe even __set() methods.</p>
<p>However, my situation is that not all the setters for my class follow this pattern so that solution means that I have to make __call() look for specific setters.</p>
<pre>
public function __call($method,$args) {
	$trapThese=array("Foo","Bar","Baz");
	preg_match('/set(*.?)/im',$method,$matches);
	if (!empty($matches)&&in_array($matches[1],$trapThese)) {
		if(is_string($args[0])||is_numeric($args[0])) {
		$propertyName=strtolower($matches[1]);
			$this->{$propertyName}=$args[0];
		}
		throw new Custom_Example_Exception("Please provide a proper string or numeric.\nException trapped in call to $method",Custom_Example_Exception::INVALID_TYPE);
	} else {
		$this->{$method}($args);
	}
}
</pre>
<p>And I'm pretty sure that this code snippet (<code>$this->{$method}($args);</code>) is the Wrong Way of Doing It. So I'll just have to settle for the redundant setFoo(),setBar,etc even though a part of me knows that there has to be a Better Way. Or maybe there is just No Way of Doing It Well.</p>
<p>I think it is probably only a matter of preference but in terms of testability, elegance, maintainability, whatever...what do you prefer, doing a magic __call() or making setFoo(),setBar(),setBaz,etc. </p>
 <img src="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=121" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2009/03/magic-__call-vs-multiple-setters/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

