<?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>rickogden.com &#187; object orientation</title>
	<atom:link href="http://www.rickogden.com/tag/object-orientation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rickogden.com</link>
	<description></description>
	<lastBuildDate>Mon, 13 Jun 2011 10:09:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>When Objects Act Like Arrays</title>
		<link>http://www.rickogden.com/2011/06/when-objects-act-like-arrays/</link>
		<comments>http://www.rickogden.com/2011/06/when-objects-act-like-arrays/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 10:09:11 +0000</pubDate>
		<dc:creator>Rick Ogden</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[object orientation]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.rickogden.com/?p=433</guid>
		<description><![CDATA[I&#8217;m a big advocate of object-oriented programming, even though only a few years ago I hated it and thought it was &#8220;over engineering&#8221;. I don&#8217;t believe, however, that object orientation is suitable for every circumstance. Anyway, that&#8217;s for another blog post. Objects are great, but not always the best when handling data, particularly things like [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big advocate of object-oriented programming, even though only a few years ago I hated it and thought it was &#8220;over engineering&#8221;. I don&#8217;t believe, however, that object orientation is suitable for every circumstance. Anyway, that&#8217;s for another blog post. Objects are great, but not always the best when handling data, particularly things like data collections.</p>
<p>One of the nice things about storing collections of data in arrays rather than objects is the fact that you&#8217;re able to easily loop through the collection, and extract data from the collection depending on the position. Sometimes though we may want to manipulate the collection, and store more information than just the data in the collection. This is where SPL comes in.</p>
<p>As of version 5.0 PHP has included the <a href="http://php.net/manual/en/book.spl.php" target="_blank">SPL</a> (Standard PHP Library). I am going to look at a couple of interfaces from this library in this blog post: <a href="http://php.net/manual/en/class.arrayaccess.php" target="_blank">ArrayAccess</a> and <a href="http://php.net/manual/en/class.seekableiterator.php" target="_blank">SeekableIterator</a>. These interfaces allow us to do a couple of things: SeekableIterator allows us to iterate through the object (eg. a foreach() loop), and the ArrayAccess allows us to extract data from the object as if it was an array. All this in a way that is completely controlled by the programmer.</p>
<p>Definition for the SeekableIterator:</p>
<pre class="brush: php; light: true; title: ; notranslate">
SeekableIterator extends Iterator {
/* Methods */
abstract public void seek ( int $position )

/* Inherited methods */
abstract public mixed Iterator::current ( void )
abstract public scalar Iterator::key ( void )
abstract public void Iterator::next ( void )
abstract public void Iterator::rewind ( void )
abstract public boolean Iterator::valid ( void )
}
</pre>
<p>Definition for the ArrayAccess:</p>
<pre class="brush: php; light: true; title: ; notranslate">
ArrayAccess {
/* Methods */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}
</pre>
<p>The way these work is that they require a variety of methods to appear in the class. With SeekableIterator there is generally a &#8220;position&#8221; property which is just an integer holding the current position in the collection (so normally defaults to 0). The methods that are required are: seek, current, key, next, rewind and valid. The &#8220;seek&#8221; moves the position to the number passed in as an argument, &#8220;next&#8221; method normally increments the position by one, and &#8220;rewind&#8221; resets the position back to 0. &#8220;current&#8221; returns the item at which the position property is at, &#8220;key&#8221; tells you the current value of position and &#8220;valid&#8221; checks to make sure an item exists at that position. You can then put the object in a foreach loop as if it was an array!</p>
<pre class="brush: php; light: true; title: ; notranslate">
foreach($iterableObject as $item {
     ...
}</pre>
<p>ArrayAccess requires fewer methods, and some of them are similar: &#8220;offsetExists&#8221; is the same as &#8220;valid&#8221; but rather than getting the position from the internal position property, it is passed in as a parameter. &#8220;offsetGet&#8221; returns the item at a given position and &#8220;offsetSet&#8221; sets an item to a given value. You can then treat an object like an array by using standard array notation:</p>
<pre class="brush: php; light: true; title: ; notranslate">
$item = $arrayAccessObject[0];
</pre>
<p>A single class can implement both of these interfaces without conflict, and SPL contains many more things like this. I have not written this as a tutorial as the PHP Docs are incredibly good on this subject and easy to follow.</p>
<p><a href="http://php.net/manual/en/class.seekableiterator.php" target="_blank">http://php.net/manual/en/class.seekableiterator.php</a><br />
<a href="http://php.net/manual/en/class.arrayaccess.php" target="_blank">http://php.net/manual/en/class.arrayaccess.php</a><br />
<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickogden.com/2011/06/when-objects-act-like-arrays/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>(An Extended) Beginners Guide to Object Orientation in PHP</title>
		<link>http://www.rickogden.com/2009/12/an-extended-beginners-guide-to-object-orientation-in-php/</link>
		<comments>http://www.rickogden.com/2009/12/an-extended-beginners-guide-to-object-orientation-in-php/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 08:55:44 +0000</pubDate>
		<dc:creator>Rick Ogden</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[object orientation]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpnw]]></category>
		<category><![CDATA[phpnw09]]></category>
		<category><![CDATA[presentation]]></category>

		<guid isPermaLink="false">http://www.rickogden.com/?p=201</guid>
		<description><![CDATA[Those of you who were that the PHPNW09 conference on the Sunday morning may have seen my talk on Object Oriented PHP. At the beginning of the month I gave a talk at the PHPNW user group which was based on the original talk, but slightly extended. Here are the slides]]></description>
			<content:encoded><![CDATA[<p>Those of you who were that the <a href="http://conference.phpnw.org.uk" target="_blank">PHPNW09</a> conference on the Sunday morning may have seen my talk on <a href="http://www.rickogden.com/tutorials/beginners-guide-to-object-orientation-in-php/">Object Oriented PHP</a>. At the beginning of the month I gave a talk at the <a href="http://www.phpnw.org.uk" target="_blank">PHPNW</a> user group which was based on the original talk, but slightly extended.</p>
<p><a href="http://www.rickogden.com/tutorials/an-extended-introduction-to-object-orientation-in-php/"><strong><em>Here are the slides</em></strong></a><!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickogden.com/2009/12/an-extended-beginners-guide-to-object-orientation-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Talking at PHPNW &#8211; 1st December</title>
		<link>http://www.rickogden.com/2009/11/talking-at-phpnw-1st-december/</link>
		<comments>http://www.rickogden.com/2009/11/talking-at-phpnw-1st-december/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 09:43:56 +0000</pubDate>
		<dc:creator>Rick Ogden</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[object orientation]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpnw]]></category>
		<category><![CDATA[phpnw09]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[talk]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.rickogden.com/?p=114</guid>
		<description><![CDATA[Just to let you know that at the next PHPNW meeting I will be giving an extended version of my talk: Beginners Guide to Object Orientation in PHP, which I gave at the PHPNW09 conference. For those who don&#8217;t know, this is held in Revolution on Deansgate Locks in Manchester, and starts at 7pm on [...]]]></description>
			<content:encoded><![CDATA[<p>Just to let you know that at the next <a href="http://www.phpnw.org.uk" target="_blank">PHPNW</a> meeting I will be giving an extended version of my talk: <a href="http://www.rickogden.com/tutorials/beginners-guide-to-object-orientation-in-php/">Beginners Guide to Object Orientation in PHP</a>, which I gave at the <a href="http://conference.phpnw.org.uk/" target="_blank">PHPNW09</a> conference.</p>
<p>For those who don&#8217;t know, this is held in Revolution on Deansgate Locks in Manchester, and starts at 7pm on Tuesday 1st December.</p>
<p><a href="http://upcoming.yahoo.com/event/4886579/MCR/Manchester/PHPNW-December-2009/Revolution-Deansgate/" target="_blank">Upcoming link</a><!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickogden.com/2009/11/talking-at-phpnw-1st-december/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  www.rickogden.com/tag/object-orientation/feed/ ) in 0.37845 seconds, on Feb 7th, 2012 at 12:26 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 7th, 2012 at 1:26 am UTC -->
