13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solution Overview<br />

839<br />

data, but because you want one set of interface functions to work with data that has<br />

come in via REST or SOAP, you can build your own object-oriented interface to the<br />

same data in instances of the Product class. Note that you cast the attributes from the<br />

XML into <strong>PHP</strong> variable types in the REST version.You do not use the cast operator<br />

in <strong>PHP</strong>, but without it here, you would receive object representations of each piece of<br />

data that will not be very useful to you.<br />

The Product class contains mostly accessor functions to access the data stored in its<br />

private members, so printing the entire file here is not worthwhile.The stucture of the<br />

class <strong>and</strong> constructor is worth visiting, though. Listing 33.11 contains part of the definition<br />

of Product.<br />

Listing 33.11 The Product Class Encapsulates the Information You Have About an<br />

Amazon Product<br />

class Product {<br />

private $ASIN;<br />

private $productName;<br />

private $releaseDate;<br />

private $manufacturer;<br />

private $imageUrlMedium;<br />

private $imageUrlLarge;<br />

private $listPrice;<br />

private $ourPrice;<br />

private $salesRank;<br />

private $availability;<br />

private $avgCustomerRating;<br />

private $authors = array();<br />

private $reviews = array();<br />

private $similarProducts = array();<br />

private $soap; // array returned by SOAP calls<br />

function __construct($xml) {<br />

if(METHOD=='SOAP') {<br />

$this->ASIN = $xml['ASIN'];<br />

$this->productName = $xml['ItemAttributes']['Title'];<br />

if (is_array($xml['ItemAttributes']['Author']) != "") {<br />

foreach($xml['ItemAttributes']['Author'] as $author) {<br />

$this->authors[] = $author;<br />

}<br />

} else {<br />

$this->authors[] = $xml['ItemAttributes']['Author'];<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!