<?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; php noob</title>
	<atom:link href="http://phpprotip.com/tag/php-noob/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>
		<item>
		<title>string searching, you&#8217;re doing it wrong</title>
		<link>http://phpprotip.com/2008/12/string-searching-youre-doing-it-wrong/</link>
		<comments>http://phpprotip.com/2008/12/string-searching-youre-doing-it-wrong/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 15:01:35 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[noobtip]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web dev]]></category>
		<category><![CDATA[corrected]]></category>
		<category><![CDATA[fail hammer]]></category>
		<category><![CDATA[fail whale]]></category>
		<category><![CDATA[peer evaluation]]></category>
		<category><![CDATA[php noob]]></category>
		<category><![CDATA[php noobtip]]></category>
		<category><![CDATA[php strpos tutorial]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[string search]]></category>
		<category><![CDATA[strpos]]></category>
		<category><![CDATA[tutorial review]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=86</guid>
		<description><![CDATA[First off, I'm not trying to be a complete jack-ass, just a helpful one. This article, by Stefan Ashwell, popped up on my news feed and I wanted to comment on it but can't remember my login for the site and after 10 minutes of waiting the password reset email has yet to arrive. The [...]]]></description>
			<content:encoded><![CDATA[<p>First off, I'm not trying to be a complete jack-ass, just a helpful one. <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy50b3RhbC1waHAuY29tL2FydGljbGUvMTgvcGVyZm9ybWluZy1zZWFyY2hlcy1vbi1zdHJpbmdzLXVzaW5nLXN0cnBvcy8=">This article</a>, by Stefan Ashwell, popped up on my news feed and I wanted to comment on it but can't remember my login for the site and after 10 minutes of waiting the password reset email has yet to arrive. The non-arrival of the password process is irritating me and shows in this article title and my tone. </p>
<p>In <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy50b3RhbC1waHAuY29tL2FydGljbGUvMTgvcGVyZm9ybWluZy1zZWFyY2hlcy1vbi1zdHJpbmdzLXVzaW5nLXN0cnBvcy8=">the article</a>, Stefan shows how <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzLnBocC5uZXQvbWFudWFsL2VuL2Z1bmN0aW9uLnN0cnBvcy5waHA=">strpos</a> or <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzLnBocC5uZXQvbWFudWFsL2VuL2Z1bmN0aW9uLnN0cmlwb3MucGhw">stripos</a> to locate a desired string or block of text.</p>
<p>The example he gives is as follows:</p>
<pre>
if ( stripos($sentence, 'string') ) {
   // yes it does
} else {
   // no it doesn't
}
</pre>
<p>The problem with this example is that it will yield a false positive. This is a common mistake made by a lot of people. If we examine the php manual entry further we see, "Returns the <strong>numeric</strong> position of the first occurrence of needle  in the haystack  string." The key word being numeric. This means if the string position is 0, then the expression <code>( stripos($sentence, 'string') )</code> will evaluate to false.<br />
<strong><br />
EDIT: Expanded explaination on why this can yield a false positive. If the string your searching for is at the beginning of the sentence, then the position returned is <code>0</code>. When php reaches the statement <code>if ( stripos($sentence, 'string') )</code> it interprets the return value (<code>0</code>) as false.<br />
</strong></p>
<p>Here is a more expanded example:</p>
<pre>

$eval=(stripos('The quick brown fox jumped over the lazy dog','The'));

echo '($eval) : ';
echo ($eval) ? "pass\n" : "fail whale\n";
echo '($eval==0) : ';
echo ($eval==0) ? "pass\n" : "fail whale\n";
echo '($eval===false)';
echo ($eval===false) ? "pass\n" : "fail whale\n";
echo 'eval is '.$eval."\n";

/**
output is:

($eval) : fail whale
($eval==0) : pass
($eval===false)fail whale
eval is 0

*/
</pre>
<p>As you can see, if you want to properly search for a string using <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzLnBocC5uZXQvbWFudWFsL2VuL2Z1bmN0aW9uLnN0cmlwb3MucGhw">stripos</a> or <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzLnBocC5uZXQvbWFudWFsL2VuL2Z1bmN0aW9uLnN0cnBvcy5waHA=">strpos</a>, you must test for the boolean value of false ($eval===false). Alternately, you can use <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3VzMi5waHAubmV0L21hbnVhbC9lbi9ib29rLnBjcmUucGhw">regex</a>, if you're comfortable with it. I'm of the opinion that learning some basic regex doesn't ever hurt you.</p>
<p>I want to re-iterate that I wanted to leave this whole post as a comment but that doesn't seem possible right now since that email has yet to arrive and the article requires login for commenting. </p>
 <img src="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=86" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2008/12/string-searching-youre-doing-it-wrong/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

