Extend Class with Final Constructor on PHP

后端 未结 6 1022
栀梦
栀梦 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:07

    Well, final means final. No overriding the method. Even if you ask nicely. I suggest adding a static make() method to your new class. Something like:

    class myclass extends SimpleXMLElement { 
        static function make($data, $xmlVersion='1.0', $xmlEncoding='ISO-8859-1', $rootName='root'){ 
            $obj=parent::__construct($data); 
            $obj->x=$xmlVersion;
            $obj->e=$xmlEncoding;
            $obj->r=$rootName;
    
            return $obj;
        } 
    }
    

提交回复
热议问题