<?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>amptools &#187; object comparison</title>
	<atom:link href="http://www.amptools.net/tag/object-comparison/feed" rel="self" type="application/rss+xml" />
	<link>http://www.amptools.net</link>
	<description>Simplify your life.</description>
	<lastBuildDate>Sat, 08 May 2010 07:40:52 +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>PHP lacks true object comparability</title>
		<link>http://www.amptools.net/blog/php-lacks-true-object-comparability</link>
		<comments>http://www.amptools.net/blog/php-lacks-true-object-comparability#comments</comments>
		<pubDate>Tue, 26 May 2009 02:05:47 +0000</pubDate>
		<dc:creator>Michael Herndon</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[object comparison]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.amptools.net/?p=38</guid>
		<description><![CDATA[For those of you who read the f* manual and see that section called <a href="http://us3.php.net/manual/en/language.oop5.object-comparison.php">object-comparison</a>.  You already realize that its a misnomer or you might be oblivious to another reason why objects in PHP are still second, even third class citizens. ]]></description>
			<content:encoded><![CDATA[<img class='gravatar' style='float: left; margin-right: 10px; border: none;' src='http://www.gravatar.com/avatar.php?gravatar_id=899a978b0f02774a965aec11eeb0b8f4&amp;default=http://amppotls.net' alt='No Gravatar' width=48 height=48/><p>For those of you who read the f* manual and see that section called <a href="http://us3.php.net/manual/en/language.oop5.object-comparison.php">object-comparison</a>.  You already realize that its a misnomer or you might be oblivious to another reason why objects in PHP are still second, even third class citizens. </p>
<p>Currently I&#8217;m writing a moderate set of Types for <a href="wiki/php-midori" rel="tag">Midori-Php</a> (granted i have most of it already written, i&#8217;m going back and for the first ctp just releasing the core types, which means i need to document, test, and test some mo).  And for those of you that think this is a crazy idea, <a href="http://us2.php.net/manual/en/book.spl-types.php">welp the PHP internals have started to do the same things themselves&#8230;. just lacking documentation and anything really useful at the moment</a>. This just adds to why PHP feels like the IE of programming languages and a complete blight.</p>
<p>I think its sad that PHP 5.3 is currently at RC2 as I write this post and the only thing you can really check with object is equality using ==. You can not use operators like greater than (&gt;), less than(&lt;), etc on objects in PHP.  The code below will illustrate php&#8217;s lack of true object comparison.  Though I won&#8217;t say this takes away from the whole notion that PHP is enterprise ready, it does take away from the ease of use of objects in PHP and thus could take away from its maintainability.  </p>
<pre class="brush: php;">
class Midori_Int32
{
    public $value = 0;

    public function __construct($value = 0)
    {
          $this-&gt;$value = $value;
    }
}

 $a = new Midori_Int32(45);
 $b = new Midori_Int32(10);
 //this would be legal, for shallow checking use ==, use === for deep comparisons
 $condition = ($a== $b)
// this is illegal in php
$condition2 = ($a &gt;= $b);
</pre>
<p>In Ruby (where everything is an object) they allow for operator overloading on method so you can define custom object types with comparability.  </p>
<pre class="brush: ruby;">
class MidoriInt32

  def initialize(value)
       @value = value
  end

  def === (obj)
    return @value === obj
  end

  def &gt; (obj)
    return @value &gt; obj
  end
end

a = MidoriInt32.new(3)
b = MidoriInt32.new(4)

condition = (a === b)
condition = (a &gt; b)
</pre>
<p>though it may be unknown, you can do the same in dot .net using <a href="http://msdn.microsoft.com/en-us/library/system.icomparable.aspx" rel="tag">IComparable</a>.  If you need to override things like math operators or type conversion you need to check out the wicked cool <a href="http://msdn.microsoft.com/en-us/library/s53ehcz3(VS.80).aspx" rel="tag"> operator keyword </a></p>
<pre class="brush: csharp;">
using System;

public class MidoriInt32 : IComparable
{

     public MidoriInt32(int value)
     {
         this.Value = value;
     }

     public int Value { get; set; }

     public static MidoriInt32 operator +(MidoriInt32 objA, int objB)
     {
          return new MidoriInt32(objA.Value + objB);
     }

     public static MidoriInt32 operator +(MidoriInt32 objA, MidoriInt32 objB)
     {
          return new MidoriInt32(objA.Value + objB.Value);
     }

     public int CompareTo(object obj) {
     {
          if(obj is int)
                return this.Value.CompareTo(obj);
          else
                 throw new ArgumentException(&quot;Object is not an integer&quot;);
     }
}

// test
class TestStuff
{
     public void Test()
     {
           var a = new MidoriInt32(3);
           var b = new MidoriInt32(4);
           var condition = (a &gt; b);
           var condition2 = (a === b);
      }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.amptools.net/blog/php-lacks-true-object-comparability/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->