Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend Class with Final Constructor on PHP

Tags:

php

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 SimpleXMLElement {
        function __construct($xmlVersion='1.0', $xmlEncoding='ISO-8859-1', $rootName='root'){
            parent::__construct("<?xml version='$xmlVersion' encoding='$xmlEncoding'?><$rootName />");
        }

I get error:

Fatal error: Cannot override final method SimpleXMLElement::__construct()

When i delete constructor i get this error:

Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects at least 1 parameter, 0 given'

I miss something or doesn't understand how properly call parent constructor which is final. I don't want override methods just expand class, but i can't expand because it required __construct(). So i missed something and back where started.

Can somebody explain where i was wrong?

like image 286
user9440008 Avatar asked Dec 03 '10 16:12

user9440008


People also ask

Can you extend final class in PHP?

The final keyword prevents child classes from overriding a method or constant by prefixing the definition with final . If the class itself is being defined final then it cannot be extended.

Can you extend final defined class?

The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class. If you try it gives you a compile time error.

What are the __ construct () and __ destruct () methods in a PHP class?

Example# __construct() is the most common magic method in PHP, because it is used to set up a class when it is initialized. The opposite of the __construct() method is the __destruct() method. This method is called when there are no more references to an object that you created or when you force its deletion.

Can final method be extended?

Final method in java can be extended but alongside the main concept to be taken into consideration is that extend means that you can extend that particular class or not which is having final method, but you can not override that final method.


1 Answers

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
    }
}
like image 136
vcardillo Avatar answered Oct 06 '22 00:10

vcardillo