Php 如何避免在方法中验证传递的参数?适当的职业发展
编写一组解析器,这些解析器依赖于一个实现共享方法的抽象类,并要求实现包含每个解析器逻辑的加法 抽象解析器代码:Php 如何避免在方法中验证传递的参数?适当的职业发展,php,oop,xml-parsing,Php,Oop,Xml Parsing,编写一组解析器,这些解析器依赖于一个实现共享方法的抽象类,并要求实现包含每个解析器逻辑的加法 抽象解析器代码: <? abstract class AbstractParser { /* * The only abstract method to implement. It contains unique logic of each feed passed to the function */ public abstract function p
<?
abstract class AbstractParser {
/*
* The only abstract method to implement. It contains unique logic of each feed passed to the function
*/
public abstract function parse($xmlObject);
/**
* @param $feed string
* @return SimpleXMLElement
* @throws Exception
*/
public function getFeedXml($feed) {
$xml = simplexml_load_file($feed);
return $xml;
}
/**
* @return array
*/
public function getParsedData() {
return $this->data;
}
/**
* @param SimpleXMLElement
* @return Array
*/
public function getAttributes($object) {
// implementation here
}
}
如您所见,我将getFeedXml方法的结果传递给parse方法,基本上将getFeedXml结果的验证委托给parse方法。
如何避免它,在我将其传递给parse方法之前,确保它返回正确的XML对象?
增加实例化过程和调用方法的数量导致需要一些工厂方法
不管怎样,你将如何解决这个小问题
谢谢 使
parse
受保护,以便只有parse\u xml\u文件
调用它:
abstract class AbstractParser {
/*
* The only abstract method to implement. It contains unique logic of each feed passed to the function
*/
protected abstract function parse($xmlObject);
/**
* @param $feed string
* @return [whatever .parse returns]
* @throws Exception
*/
public function parseFile($feed) {
$xml = simplexml_load_file($feed);
if (!$xml) {
throw new \Exception('Unable to load remote XML feed');
}
return $this->parse($xml);
}
/**
* @return array
*/
public function getParsedData() {
return $this->data;
}
/**
* @param SimpleXMLElement
* @return Array
*/
public function getAttributes($object) {
// implementation here
}
}
$parser = new FormulaDrivers();
$parser->parse( $parser->getFeedXml('http://api.xmlfeeds.com/formula_drivers.xml') );
abstract class AbstractParser {
/*
* The only abstract method to implement. It contains unique logic of each feed passed to the function
*/
protected abstract function parse($xmlObject);
/**
* @param $feed string
* @return [whatever .parse returns]
* @throws Exception
*/
public function parseFile($feed) {
$xml = simplexml_load_file($feed);
if (!$xml) {
throw new \Exception('Unable to load remote XML feed');
}
return $this->parse($xml);
}
/**
* @return array
*/
public function getParsedData() {
return $this->data;
}
/**
* @param SimpleXMLElement
* @return Array
*/
public function getAttributes($object) {
// implementation here
}
}
$parser->parseFile('http://api.xmlfeeds.com/formula_drivers.xml');