最后让我们看一个简单的实际例子来理解
PHP5风格的SoapClient这个服务
。假设有这样的一个例子,我们需要查看美国伊利诺斯州的moline的天气
。这个获得当前moline飞机场天气状态的代码称为”KMLI”,需要调用getWeatherReport()方法和传递’KMLI’字符串作为参数。这个调用将返回一个WeatherReport对象。
classProxyTestCaseextendsUnitTestCase{
functionTestGetWeatherReport(){
$moline_weather=$this->client->getWeatherReport(‘KMLI’);
$this->assertIsA($moline_weather,‘stdClass’);
}
}
因为WeatherReport实际上并不是你程序中定义的类,SoapClient都象stdClass的实例化一样的返回所有的对象。这时你也可以获得返回对象的属性的值。
classProxyTestCaseextendsUnitTestCase{
functionTestGetWeatherReport(){
$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_testsas$key=>$isa){
$this->assertIsA($moline_weather->$key,
$isa,
“$keyshouldbe$isa,actually[%s]”);
}
}
}
上面的代码创建了属性和返回类型的映射。你可以迭代这些预期值的列表,并使用assertIsA()验证正确的类型。当然你以可以同样的验证其他的集合对象。
classProxyTestCaseextendsUnitTestCase{
functionTestGetWeatherReport(){
//continued...
$temp=$moline_weather->temperature;
$temperature_tests=array(
‘ambient’=>‘Float’
,’dewpoint’=>‘Float’
,’relative_humidity’=>‘Integer’
,’string’=>‘String’
);
foreach($temperature_testsas$key=>$isa){
$this->assertIsA($temp->$key,
$isa,
“$keyshouldbe$isa,actually[%s]”);
}
}
}
上面的方法输出的实际效果如下:
stdClassObject
(
[timestamp]=>2005-02-27T13:52:00Z
[station]=>stdClassObject
(
[icao]=>KMLI
[wmo]=>72544
[iata]=>
[elevation]=>179
[latitude]=>41.451
[longitude]=>-90.515
[name]=>Moline,Quad-CityAirport
[region]=>IL
[country]=>UnitedStates
[string]=>KMLI-Moline,Quad-CityAirport,IL,UnitedStates@41.451’N-90.515’W179m
)
//...
[temperature]=>stdClassObject
(
[ambient]=>0.6
[dewpoint]=>-2.8
[relative_humidity]=>78
[string]=>0.6c(78%RH)
)
//...
)
希望这篇
php教程:php设计模式介绍之代理模式(3)的文章能够对您有所帮助,如果您觉得这篇网站维护教程有用的话,别忘了推荐给您的朋友哦!如果您有好的经验方法,不妨拿出来和大家一起分享:假如每个人都拿出一个经验,那么我们都将额外的获取一堆他人的经验。
请记住本站永久域名:(黑客防线网安服务器维护方案维护基地)Www.Rongsen.Com.Cn