我正在研究验证帐号的方法。 $ account_number从另一个函数传入以进行验证。我遇到了变量的范围问题,从函数传递到类。我有它工作,但我使用$ GLOBALS来解决范围问题。我觉得必须有更好的方法。这就是我所拥有的:
$acct;
$subAcct;
$chart;
$object;
$subObject;
$project;
function verifyACCT($account_number){
//Strip all but numbers and letters, truncate to first seven digits, and convert to uppercase
$account_number = strtoupper(preg_replace("/[^a-z0-9]/i", "", $account_number));
$GLOBALS['$acct'] = substr($account_number, 0, 7);
$GLOBALS['$subAcct'] = substr($account_number, 8);
$GLOBALS['$chart'] = "XX";
$GLOBALS['$object'] = "0000";
$GLOBALS['$subObject'] = null;
$GLOBALS['$project'] = null;
class ACCTSoapClient extends SoapClient {
public function __doRequest($request, $location, $action, $version, $one_way=0) {
$request = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<isValidAccountString xmlns="http://URL/">
<chartOfAccountsCode xmlns="">'.$GLOBALS['$chart'].'</chartOfAccountsCode>
<accountNumber xmlns="">'.$GLOBALS['$acct'].'</accountNumber>
<subAccountNumber xmlns="">'.$GLOBALS['$subAcct'].'</subAccountNumber>
<objectCode xmlns="">'.$GLOBALS['$object'].'</objectCode>
<subObjectCode xmlns="">'.$GLOBALS['$subObject'].'</subObjectCode>
<projectCode xmlns="">'.$GLOBALS['$project'].'</projectCode>
</isValidAccountString>
</soap:Body>
</soap:Envelope>';
return parent::__doRequest($request, $location, $action, $version, $one_way);
}
}
$client = new ACCTSoapClient("https://URL?wsdl", array("connection_timeout"=>5, 'exceptions' => 0));
try {
$result = $client->isValidAccountString(null);
return ($result->return); //boolean (1 for valid, null for invalid)
} catch(SoapFault $e) {
echo 1;
} catch(Exception $e) {
echo 1;
}
}
答案 0 :(得分:1)
为了绕过使用$ _GLOBALS(这被认为是非常糟糕的做法),您需要重构您的代码。
您目前正在将过程代码方法与面向对象编程混合使用。
protected
或private
将您当前的全局变量放入新验证类。public function
verify
verify
方法verify
方法将调用SOAP类的doRequest
方法并返回响应我希望这有助于您带领您走上正轨。