Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.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

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_Design Patterns - Fatal编程技术网

Php 重写方法以从派生子类获取参数

Php 重写方法以从派生子类获取参数,php,oop,design-patterns,Php,Oop,Design Patterns,我有几次以一个解决方案结束,其中我有两个类的层次结构。其中,第一(迎宾)层级的类使用第二(用户)层级的类 这里有一个例子: class User {} class Admin extends User { function getSecretMessage() { return "secret"; } } class Greeter { public function hello(User $a) { echo "hello!";

我有几次以一个解决方案结束,其中我有两个类的层次结构。其中,第一(迎宾)层级的类使用第二(用户)层级的类

这里有一个例子:

class User {}
class Admin extends User {
    function getSecretMessage() {
        return "secret";
    }
}

class Greeter {
    public function hello(User $a) {
        echo "hello!";
    }
}

class AdminGreeter extends Greeter {
    public function hello(Admin $a) {
        parent::hello($a);
        echo "in addition heres a secret of mine: " . $a->getSecretMessage();
    }
}
这里我有用户和迎宾者,在PHP中我收到错误(严格)

“AdminGreeter::hello的声明必须与Greeter::hello兼容”

我希望
AdminGreeter::hello
使用更专业的类(Admin)的数据简单地“扩展”
Greeter::hello

我有什么选择来构建类似的PHP

我想主要的问题是PHP不支持“方法重载”,因此,如果我向
AdminGreeter
发送
User
实例,它就会崩溃。但是,如果我有“方法重载”,那么如果在用户实例中传递,
Greeter::hello
将被简单地调用

这可能是一个很糟糕的设计,因为我最终遇到了这个问题,也许有人可以为我指出一个更好的设计来解决这个问题


当我开发Objut-C

时,我可能会遇到同样的问题,也许你可以考虑使用这些“特殊类”< /P>


要尽可能保留原始代码,请执行以下操作:

interface iUser {
    public function getSecretMessage();
}

class Admin implements iUser {
    function getSecretMessage() {
        return 'secrete';
    }
}

class Greeter implements iUser{
    public function hello( iUser $a ) {
        echo 'hello';
    }

    public function getSecretMessage(){}
}

class AdminGreeter extends Greeter {
    public function hello( iUser $a ) {
        parent::hello($a);
        echo ' in addition heres a secrete of mine: ' . $a->getSecretMessage();
    }
}

AdminGreeter::hello( new Admin );

输出<代码>你好,另外还有我的一个秘密:secret

Admin
不是一个类,这是一个感谢你的权利,我应该正确地使用“类型”而不是“类”这个词。我对问题进行了编辑,以消除任何混淆:)如果您想重用代码,则可能会出现重复。通常,继承并不是解决方法。看起来你想实现一些类似访客模式的东西看看decorator,你是对的,decorator模式更适合我的问题。我觉得保持多态接口很重要,因此我可以在每个问候者上调用::hello并注入User类型的实例。我明白你为什么建议访问者模式,但是对于我的问题(这里大大简化了),装饰师是一个很好的解决方案,谢谢请提供答案:)否则我将使用我创建的解决方案创建答案。
interface iUser {
    public function getSecretMessage();
}

class Admin implements iUser {
    function getSecretMessage() {
        return 'secrete';
    }
}

class Greeter implements iUser{
    public function hello( iUser $a ) {
        echo 'hello';
    }

    public function getSecretMessage(){}
}

class AdminGreeter extends Greeter {
    public function hello( iUser $a ) {
        parent::hello($a);
        echo ' in addition heres a secrete of mine: ' . $a->getSecretMessage();
    }
}

AdminGreeter::hello( new Admin );