Extend Class with Final Constructor on PHP

后端 未结 6 1023
栀梦
栀梦 2021-01-06 02:43

I want extend class which have final constructor (in my case it\'s SimpleXMLElement), but i have problems because when i use:

    class myclass extends Simpl         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-06 03:19

    I just went through this exact thing. You don't need to extend it. Make a class that holds SimpleXMLElement objects. I believe this is what Nikola meant.

    class XmlResultSet
    {
        public $xmlObjs = array();
    
        public function __construct(array $xmlFiles)
        {
          foreach ($xmlFiles as $file) {
              $this->xmlObjs[] = new XmlResult($file);
          }
        }
    }
    
    class XmlResult
    {
        private $xmlObj;
    
        public function __construct($file)
        {
            try {
                $this->xmlObj = new SimpleXMLElement($file, 0, true);
            }
            catch (Exception $e) {
                throw new MyException("Invalid argument ($this)($file)(" . $e .
                ")", PHP_ERRORS);
            }
        }
    
        public function otherFunctions()
        {
            return $this->xmlObj->movie['name']; // whatever
        }
    }
    

提交回复
热议问题