Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何避免在方法中验证传递的参数?适当的职业发展_Php_Oop_Xml Parsing - Fatal编程技术网

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');