<?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>Viktors Rotanovs &#187; oop</title>
	<atom:link href="http://rotanovs.com/tag/oop/feed/" rel="self" type="application/rss+xml" />
	<link>http://rotanovs.com</link>
	<description></description>
	<lastBuildDate>Thu, 13 May 2010 21:52:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>jQuery-style Chainability in PHP, Using SPL</title>
		<link>http://rotanovs.com/web-developer/jquery-chains-php-spl/</link>
		<comments>http://rotanovs.com/web-developer/jquery-chains-php-spl/#comments</comments>
		<pubDate>Mon, 29 Oct 2007 01:18:54 +0000</pubDate>
		<dc:creator>viktors</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ArrayAccess]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spl]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://localhost/web-developer/2007/10/29/jquery-style-chainability-in-php-using-spl/</guid>
		<description><![CDATA[Hacking jQuery-style chainable array operators into PHP, without modifying Zend core.]]></description>
			<content:encoded><![CDATA[<p>What could be better than chainable operators in jQuery, .addClass(&#8221;beautiful&#8221;).show(&#8221;slow&#8221;)-style? Almost nothing, but adding those to PHP would make code cleaner and life easier.</p>
<p>At first sight, implementing these looks almost impossible, but after a quick look at extensions we notice that <a href="http://www.php.net/~helly/php/ext/spl/">SPL</a> could offer something similar, if combined with <a href="http://es.php.net/manual/en/language.oop5.magic.php">magic methods</a>. Let&#8217;s implement ArrayAccess interface and see how it works:</p>
<pre line="1" lang="php">
$fruits = new Chain(array(
           new String('apple'),
           new String('mango'),
           new String('kiwi')
)); 

echo "First fruit: ";
echo $fruits[0];
echo "\n"; 

echo "All fruits: ";
echo $fruits->toUpperCase()->join();
echo "\n";</pre>
<p>Outputs:</p>
<pre>
First fruit: apple
All fruits: APPLE, MANGO, KIWI</pre>
<p>Beautiful, eh?</p>
<p>Here is a complete listing:</p>
<pre line="1" lang="php">

class Chain implements ArrayAccess {
    private $items = array(); 

    public function __construct($items = array()) {
      if (!is_array($items)) return;
      $this->items = $items;
    } 

    public function add($element) {
      $this->items[] = $element;
    } 

    public function __call($method, $args) {
      if (count($this->items)
            &#038;&#038; !method_exists($this->items[0], $method)) {
        throw new BadMethodCallException();
      } 

      $returnChain = new Chain();
      foreach($this->items as $item) {
        $returnChain->add(call_user_func_array(array($item, $method), $args));
      }
      return $returnChain;
    } 

    public function rewind() {
      reset($this->items);
    } 

    public function current() {
      return current($this->items);
    }
   public function key() {
      return key($this->items);
    } 

    public function next() {
      return next($this->items);
    } 

    public function valid() {
      return $this->current() !== false;
    } 

    public function offsetExists($offset) {
      return isset($this->items[$offset]);
    } 

    public function offsetGet($offset) {
      return $this->items[$offset];
    } 

    public function offsetSet($offset, $value) {
      return $this->items[$offset] = $value;
    } 

    public function offsetUnset($offset) {
      unset($this->items[$offset]);
    } 

    // convenience method
    public function join($separator = ', ') {
      return implode($this->items, $separator);
    }
} 

class String{
    private $s; 

    public function __construct($s) {
      $this->s = $s;
    } 

    public function toUpperCase() {
      $this->s = strtoupper($this->s);
      return $this;
    } 

    public function __toString() {
      return $this->s;
    } 

} 

$fruits = new Chain(array(
              new String('apple'),
              new String('mango'),
              new String('kiwi')
)); 

echo "First fruit: ";
echo $fruits[0];
echo "\n"; 

echo "All fruits: ";
echo $fruits->toUpperCase()->join();
echo "\n";</pre>
]]></content:encoded>
			<wfw:commentRss>http://rotanovs.com/web-developer/jquery-chains-php-spl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
