OSOA 聯盟推出的 SCA(Service Component Architecture)與 SDO(Service Data Objects)現已移植至 PHP,使 PHP 能參與企業級 SOA 生態。
傳統 PHP 應用直接操作 MySQL、XML、Web Service 等異構源,代碼耦合度高。SDO 引入 Data Access Service (DAS) 作為中介層,業務代碼僅面向統一的 SDO_DataObject 接口:
<?php
$providers = $company->shippingByGround;
foreach ($providers as $name => $value) {
echo "$name: $value\n";
}
?>
上述代碼不知數據來自關係庫、XML 還是遠程服務,細節由 DAS 封裝。PHP 目前內建 XML DAS 與 RDBMS DAS。
SCA 通過註解聲明服務綁定,屏蔽調用細節(本地類、遠程 WSDL、異語言實現):
<?php
include "SCA/SCA.php";
/**
* @service
*/
class ShipmentQuote {
/**
* @reference
* @binding.php ../DiscountFeeRate/DiscountFeeRate.php
*/
public $discountFee;
/**
* @reference
* @binding.wsdl ../Shipper/ShipperQuote.wsdl
*/
public $shipper;
/**
* @param string $shipping
* @param string $customer
* @return float
*/
function getQuote($shippingCo, $customer) {
$rateShip = $this->shipper->getShippingPrice($shippingCo);
$rate = $this->discountFee->getDiscountRate($customer);
return $rate * $rateShip;
}
}
?>
@service:暴露類為服務,自動生成 WSDL 端點@reference + @binding.php:注入本地 PHP 類依賴@binding.wsdl:注入遠程 SOAP 服務依賴getQuote 完全無感知依賴的物理位置與協議