<?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>Adam Trachtenberg &#187; upgrading to php 5</title>
	<atom:link href="http://www.trachtenberg.com/blog/tag/upgrading-to-php-5/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.trachtenberg.com/blog</link>
	<description>Thoughts on PHP, eBay, and too many technical topics for my family's liking.</description>
	<lastBuildDate>Tue, 24 Aug 2010 21:39:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Did you know about PHP&#8217;s old_function?</title>
		<link>http://www.trachtenberg.com/blog/2006/04/05/did-you-know-about-phps-old_function/</link>
		<comments>http://www.trachtenberg.com/blog/2006/04/05/did-you-know-about-phps-old_function/#comments</comments>
		<pubDate>Wed, 05 Apr 2006 23:52:04 +0000</pubDate>
		<dc:creator>Adam Trachtenberg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[upgrading to php 5]]></category>

		<guid isPermaLink="false">http://www.trachtenberg.com/blog/2006/04/05/did-you-know-about-phps-old_function/</guid>
		<description><![CDATA[I was reviewing Upgrading to PHP 5 today and I came across this lovely section in Appendix B, which I had completely forgotten about:
PHP/FI had a quirky function declaration syntax:
function sum $a, $b (
    return($a + $b);
);

This was changed in PHP 3, but you could continue to use the old form in [...]]]></description>
			<content:encoded><![CDATA[<p>I was reviewing <em><a href="http://www.trachtenberg.com/blog/2004/05/15/upgrading-to-php-5/">Upgrading to PHP 5</a></em> today and I came across this lovely section in Appendix B, which I had completely forgotten about:</p>
<blockquote><p>PHP/FI had a quirky function declaration syntax:</p>
<pre><code>function sum $a, $b (
    return($a + $b);
);
</code></pre>
<p>This was changed in PHP 3, but you could continue to use the old form in PHP 3 and PHP 4 if you declared your function as an old_function:</p>
<pre><code>old_function sum $a, $b (
    return($a + $b);
);
</code></pre>
<p>Alas, after six years, this backward compatibility feature is now gone. Another nostalgic remnant of PHP/FI has passed away.</p></blockquote>
<p>I must say, I used PHP/FI, but I never resorted to the <code>old_function</code> hack to ease my PHP 3 migration issues.</p>
<p>Did anybody ever use this?</p>
<img src="http://www.trachtenberg.com/blog/?ak_action=api_record_view&id=502&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.trachtenberg.com/blog/2006/04/05/did-you-know-about-phps-old_function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Auctioning Autographed &#8220;Upgrading to PHP 5&#8243; for the EFF</title>
		<link>http://www.trachtenberg.com/blog/2006/02/11/auctioning-autographed-upgrading-to-php-5-for-the-eff/</link>
		<comments>http://www.trachtenberg.com/blog/2006/02/11/auctioning-autographed-upgrading-to-php-5-for-the-eff/#comments</comments>
		<pubDate>Sun, 12 Feb 2006 02:18:10 +0000</pubDate>
		<dc:creator>Adam Trachtenberg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[charity]]></category>
		<category><![CDATA[ebay]]></category>
		<category><![CDATA[eff]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[upgrading to php 5]]></category>

		<guid isPermaLink="false">http://www.trachtenberg.com/blog/2006/02/11/auctioning-autographed-upgrading-to-php-5-for-the-eff/</guid>
		<description><![CDATA[Every since I started at eBay, I&#8217;ve been looking to sell more on the site. I&#8217;ve also been looking to donate more money to charity. So I decided to combine the two: I&#8217;m selling a copy of Upgrading to PHP 5  on eBay and donating everything to the EFF.
The EFF, short for Electronic Frontier [...]]]></description>
			<content:encoded><![CDATA[<p>Every since I started at eBay, I&#8217;ve been looking to sell more on the site. I&#8217;ve also been looking to donate more money to charity. So I decided to combine the two: I&#8217;m selling a copy of <em><a href="http://www.oreilly.com/catalog/upgradephp5/">Upgrading to PHP 5 </a></em> on eBay and donating everything to the <a href="http://www.eff.org/">EFF</a>.</p>
<p>The EFF, short for Electronic Frontier Foundation, describes themselves as:</p>
<blockquote><p>[A] group of passionate people â€” lawyers, technologists, volunteers, and visionaries â€” working in the trenches, battling to protect your rights and the rights of web surfers everywhere. The dedicated people of EFF challenge legislation that threatens to put a price on what is invaluable; to control what must remain boundless.</p></blockquote>
<p>List priced at $29.99, the auction starts at $0.01, so you&#8217;re guaranteed to get a good value. As an extra special bonus, I will custom autograph the book to the winning bidder or a friend. Or, for an additional $5, I will not sully up your clean new book with my autograph. Your choice. Okay, just kidding about the $5.</p>
<p>If you&#8217;ve been looking to get up to speed on PHP 5, now&#8217;s the perfect chance to buy a book and give money to a great cause. Check out my listing and <a href="http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&#038;item=4612780421&#038;rd=1&#038;sspagename=STRK%3AMESE%3AIT&#038;rd=1">bid today</a>.</p>
<img src="http://www.trachtenberg.com/blog/?ak_action=api_record_view&id=452&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.trachtenberg.com/blog/2006/02/11/auctioning-autographed-upgrading-to-php-5-for-the-eff/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>eBay SOAP Update: Syntax Matters</title>
		<link>http://www.trachtenberg.com/blog/2005/10/24/ebay-soap-update/</link>
		<comments>http://www.trachtenberg.com/blog/2005/10/24/ebay-soap-update/#comments</comments>
		<pubDate>Mon, 24 Oct 2005 07:04:48 +0000</pubDate>
		<dc:creator>Adam Trachtenberg</dc:creator>
				<category><![CDATA[Web Services]]></category>
		<category><![CDATA[ebay]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[upgrading to php 5]]></category>

		<guid isPermaLink="false">http://www.trachtenberg.com/blog/2005/10/23/ebay-soap-update/</guid>
		<description><![CDATA[I saw a request for some actual SOAP code, so I will try and oblige. I don&#8217;t want to publish the entire code because there&#8217;s lots of messy stuff that&#8217;s specific to eBay&#8217;s SOAP API. I&#8217;ll talk about that some other time, but for now, those details just get in the way.
Therefore, I&#8217;ll pull out [...]]]></description>
			<content:encoded><![CDATA[<p>I saw <a href="http://www.phpdeveloper.org/news/4128">a request for some actual SOAP code</a>, so I will try and oblige. I don&#8217;t want to publish the entire code because there&#8217;s lots of messy stuff that&#8217;s specific to eBay&#8217;s SOAP API. I&#8217;ll talk about that some other time, but for now, those details just get in the way.</p>
<p>Therefore, I&#8217;ll pull out a few lines, combine that with some hand waving, and just hope for the best. Here&#8217;s the before:</p>
<pre><code>
$wsdl = 'http://developer.ebay.com/webservices/latest/eBaySvc.wsdl';
$client = new SoapClient($wsdl);

$params = array('Query' => 'ipod');
$results = $client->GetSearchResults($params);

foreach ($results->SearchResultItemArray->SearchResultItem as $item) {
  print $item->Item->Title . &quot;\n&quot;;
}
</code></pre>
<p>Without the <tt>classmap</tt> option, I create a <tt>SoapClient</tt>, make my request, and then iterate through the results to print out the titles of the matching items. This isn&#8217;t complex; however, the iteration is a little klunky due to limitations of SOAP and the design of eBay&#8217;s Web service.</p>
<p>For example, the <tt>SearchResultItemArray</tt> only contains <tt>SearchResultItem</tt>, so it&#8217;s kludgy to reference <tt>SearchResultItemArray->SearchResultItem</tt>. Likewise, when I&#8217;m just getting a quick dump of <tt>Item</tt> information, it&#8217;s not so nice to specifically access the <tt>Title</tt> element.</p>
<p>By defining a couple of classes and telling the SoapClient object map them to the return data, I can clean this up:</p>
<pre><code>
class eBaySearchResultItemArrayType implements IteratorAggregate {
  public function getIterator( ) {
    return new ArrayObject($this->SearchResultItem);
  }
}

class eBaySearchResultItemType {
  public function __toString() {
    return $this->Item->Title . &quot;\n&quot;;
  }
}

$wsdl = 'http://developer.ebay.com/webservices/latest/eBaySvc.wsdl';
$options = array('classmap' => array(
  'SearchResultItemArrayType' => 'eBaySearchResultItemArrayType',
  'SearchResultItemType' => 'eBaySearchResultItemType',
  ),
);

$client = new SoapClient($wsdl, $options);

$params = array('Query' => 'ipod');
$results = $client->GetSearchResults($params);

foreach ($results->SearchResultItemArray as $item) {
  print $item;
}
</code></pre>
<p>To solve my first problem, the iteration, I make  <tt>eBaySearchResultItemArrayType</tt> implement the <tt>IteratorAggregate</tt> interface. When a PHP 5 class implements this interface, PHP will invoke the <tt>getIterator()</tt> method during a <tt>foreach</tt> loop.</p>
<p>In this case, I return <tt>$this->SearchResultItem</tt>, wrapping it inside an <tt>ArrayObject</tt> to make the array iterable.</p>
<p>For the pretty-printing issue, I define a <tt>__toString()</tt> method inside of <tt>eBaySearchResultItemType</tt>. Now, when I print an instance of this class, PHP calls that method instead.</p>
<p>With my classes defined, I use the <tt>classmap</tt> option to map the PHP classes to the SOAP complexTypes, and pass this mapping along as part of the second parameter to the <tt>SoapClient</tt> constructor.</p>
<p>Once this is set up, everything else in the request is identical. However, when I print out the results, the syntax is clean:</p>
<pre><code>
foreach ($results->SearchResultItemArray as $item) {
  print $item;
}
</code></pre>
<p>At one level, this is just syntax and icing. However, I don&#8217;t think you should dismiss syntax with a wave of your hand. To quote Sam Ruby on <a href="http://www.intertwingly.net/blog/2005/09/29/Ruby-1-8-vs-LINQ">C# and LINQ</a>:</p>
<blockquote><p>[S]yntax matters.  Very much so.</p></blockquote>
<p>So, I am convinced that this is a good thing to spend time on. There are a couple other tricks I&#8217;ve pulled out, such as:</p>
<pre><code>
class eBayFeesType implements ArrayAccess {

  public function offsetGet($offset) {
    foreach ($this->Fee as $value) {
      if ($value->Name == $offset) {
        return $value;
      }
    }
  }

  /* and the other interface methods... */
}

class eBayFeeType {
  public function __toString() {
    return (string) $this->Fee->_;
  }
}
</code></pre>
<p>This lets me do:</p>
<pre><code>
echo &quot;Listing fee is: &quot;, $results->Fees['ListingFee'], &quot;\n&quot;;
</code></pre>
<p>Which I must say is far nicer than either iterating in place or even calling out to a utility method.</p>
<p>I was interested to discover that I needed to manually cast <tt>$this->Fee->_</tt> to a string because I thought it was a string. Actually, it is a float, and PHP won&#8217;t autocast floats to strings in this instance.</p>
<p>Personally, I think if PHP should autocast here, but I can see the logic. If you&#8217;re promising to return a string, you should actually return one.</p>
<p>At first, I was writing each class by hand. But that got boring and wasn&#8217;t scalable. Therefore, my new goal is to automate this process. I am writing a script to read the WSDL file, parse out the complexTypes, and then convert them to PHP classes.</p>
<p>My original script was using the <tt>SoapClient::__getTypes()</tt> method as a pre-processor, but I needed to parse that output again to get it into a usable format. After a little digging, I discovered that PEAR::SOAP implements its own parsing routines and gives me a far more flexible PHP data structure to manipulate. So, I am going in that direction instead.</p>
<p>Since this is a one-time action, speed isn&#8217;t vital, so it&#8217;s okay that PEAR::SOAP is written in PHP.</p>
<p>If you&#8217;re interested in more details on these new PHP 5 features, they&#8217;re documented in the <a href="http://www.php.net/manual">PHP Manual</a>. Alternatively, in a shameless plug, you can check out my book <a href="http://www.amazon.com/exec/obidos/ASIN/0596006365/ref=nosim/trachtenberg-20">Upgrading to PHP 5</a>.</p>
<img src="http://www.trachtenberg.com/blog/?ak_action=api_record_view&id=325&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.trachtenberg.com/blog/2005/10/24/ebay-soap-update/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

