在PHP上使用最终构造函数扩展类 [英] Extend Class with Final Constructor on PHP

查看:90
本文介绍了在PHP上使用最终构造函数扩展类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要具有最终构造函数的扩展类(在我的情况下是SimpleXMLElement),但是我遇到了问题,因为当我使用时:

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 />");
        }

我收到错误消息:

致命错误:无法覆盖最终结果 方法SimpleXMLElement :: __ construct()

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

当我删除构造函数时,出现此错误:

When i delete constructor i get this error:

致命错误:未捕获的异常 带有消息的异常" 'SimpleXMLElement :: __ construct() 至少需要1个参数,给定0个参数.

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

我错过了一些事情,或者不了解如何正确调用最终的父构造函数. 我不希望重写方法只是扩展类,但是我不能扩展,因为它需要__construct().所以我错过了一些东西,然后回到了起点.

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?

推荐答案

在这种情况下,我将使用Delegate包装器设计.您应该在这里考虑使用组合而不是继承.

I would use Delegate wrapper design in this case. You should consider composition instead of inheritance here.

这篇关于在PHP上使用最终构造函数扩展类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆