最后让我们看一个简单的实际例子来理解PHP5风格的SoapClient这个服务。假设有这样的一个例子,我们需要查看美国伊利诺斯州的moline的天气。这个获得当前moline飞机场天气状态的代码称为”KMLI”,需要调用getWeatherReport()方法和传递’KMLI’字符串作为参数。这个调用将返回一个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]”); } } }
上面的代码创建了属性和返回类型的映射。你可以迭代这些预期值的列表,并使用assertIsA()验证正确的类型。当然你以可以同样的验证其他的集合对象。
class ProxyTestCase extends UnitTestCase { function TestGetWeatherReport() { // continued ... $temp = $moline_weather->temperature; $temperature_tests = array( ‘ambient’ => ‘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
责任编辑:bluehearts
上一页 php设计模式介绍之章代理模式 [2] 下一页 php设计模式介绍之章代理模式 [4]
◎进入论坛网络编程版块参加讨论
|