最后讓我們看一個簡單的實際例子來理解PHP5風(fēng)格的SoapClient這個服務(wù)。假設(shè)有這樣的一個例子,我們需要查看美國伊利諾斯州的moline的天氣。這個獲得當前moline飛機場天氣狀態(tài)的代碼稱為”KMLI”,需要調(diào)用getWeatherReport()方法和傳遞’KMLI’字符串作為參數(shù)。這個調(diào)用將返回一個WeatherReport對象。
class ProxyTestCase extends UnitTestCase { function TestGetWeatherReport() { $moline_weather = $this->client->getWeatherReport(‘KMLI’); $this->assertIsA($moline_weather, ‘stdClass’); } }
因為WeatherReport實際上并不是你程序中定義的類, SoapClient都象stdClass的實例化一樣的返回所有的對象。這時你也可以獲得返回對象的屬性的值。
class ProxyTestCase extends UnitTestCase { function TestGetWeatherReport() { $moline_weather = $this->client->getWeatherReport(‘KMLI’); $this->assertIsA($moline_weather, ‘stdClass’); $weather_tests = array( ‘timestamp’ => ‘String’ ,’station’ => ‘stdClass’ ,’phenomena’ => ‘Array’ ,’precipitation’ => ‘Array’ ,’extremes’ => ‘Array’ ,’pressure’ => ‘stdClass’ ,’sky’ => ‘stdClass’ ,’temperature’ => ‘stdClass’ ,’visibility’ => ‘stdClass’ ,’wind’ => ‘stdClass’ ); foreach($weather_tests as $key => $isa) { $this->assertIsA($moline_weather->$key, $isa, “$key should be $isa, actually [%s]”); } } }
上面的代碼創(chuàng)建了屬性和返回類型的映射。你可以迭代這些預(yù)期值的列表,并使用assertIsA()驗證正確的類型。當然你以可以同樣的驗證其他的集合對象。
class ProxyTestCase extends UnitTestCase { function TestGetWeatherReport() { // continued ... $temp = $moline_weather->temperature; $temperature_tests = array( ‘a(chǎn)mbient’ => ‘Float’ ,’dewpoint’ => ‘Float’ ,’relative_humidity’ => ‘Integer’ ,’string’ => ‘String’ ); foreach($temperature_tests as $key => $isa) { $this->assertIsA($temp->$key, $isa, “$key should be $isa, actually [%s]”); } } }
上面的方法輸出的實際效果如下:
stdClass Object ( [timestamp] => 2005-02-27T13:52:00Z [station] => stdClass Object ( [icao] => KMLI [wmo] => 72544 [iata] => [elevation] => 179 [latitude] => 41.451 [longitude] => -90.515 [name] => Moline, Quad-City Airport [region] => IL [country] => United States [string] => KMLI - Moline, Quad-City Airport, IL, United States @ 41.451’N -90.515’W 179m ) // ... [temperature] => stdClass Object ( [ambient] => 0.6 [dewpoint] => -2.8 [relative_humidity] => 78 [string] => 0.6c (78% RH) ) // ... )
出處:phpchina
責(zé)任編輯:bluehearts
上一頁 php設(shè)計模式介紹之章代理模式 [2] 下一頁 php設(shè)計模式介紹之章代理模式 [4]
◎進入論壇網(wǎng)絡(luò)編程版塊參加討論
|