PHP 类中的多级继承 [英] Multi-level inheritance in PHP classes

查看:28
本文介绍了PHP 类中的多级继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个代码:

class A {
   ...
}

class B extends A {
   ...
}

class C extends B {
   ...
}

$c = new C();
$c->getMethodOrPropertyFromB()->getMethodOrPropertyFromA();

除了糟糕的架构或糟糕的设计之外,这是否会对脚本执行时的 PHP/Webserver (Apache/Nginx) 性能产生任何影响?

Other than a bad architecture or bad design will this have any impact on PHP / Webserver (Apache/Nginx) performance when the script executes?

如果不建议在 PHP 类中有这样的多级,你能解释一下为什么吗?

If this is not recommended to have such multi-level in PHP classes, can you explain why?

注意:除了我得到的答案之外,我会让 这个 这里也有帮助

Note: in addition to the answers I've got I will let this here which is helpful as well

推荐答案

我最初的想法是这可能不利于继承,但经过测试似乎还可以.但是,您可能知道还有其他方法可以实现此目的.

My initial thought was that this may not be good for inheritance but after testing it seems okay. However, there are other means of accomplishing this you could be aware of.

抽象类或接口可能有意义.

Abstract classes or Interfaces may make sense.

抽象类和其他类一样,但是它们不能被实例化.还有一些抽象方法必须由具体的类来实现.

Abstract classes are just like other classes but they cannot be instantiated. There are also abstract methods which must be implemented by concrete classes.

abstract class A {
    //You can also have abstract methods
    abstract public function doFoo();
    abstract public function doBar($when);

    //Also implemented method which when
    //called unless overridden will use this logic
    public function sayHi(){
        echo "hi";
    }
}

现在这个类可以选择实现抽象方法或者不添加任何它需要的进一步的逻辑.

Now this class can choose to implement the abstract methods or not also adding any further logic needed by it.

abstract class B extends A {
    public function doFoo(){
        //Some code
    }
    abstract public function doFooBar();
    public function sayBye(){
        echo "bye";
    }
}

这是一个具体的类,如果尚未实现的抽象方法可以再次覆盖,则必须在此处实现所有抽象方法.

This is a concrete class and all abstract methods must be implemented here if not already ones that are implemented can be overridden yet again.

class C extends B {
    public function doFoo(){
        //Some different code
    }
    public function doBar($when){
        //Some code
    }
    public function doFooBar(){
        //Some code
    }

    //do not need sayHi() and  sayBye() but they will be available.
}

简单粗暴的接口就是一堆方法.您只是告诉开发人员您是否要使用此实现这些.这些方法没有声明为抽象的,但不能在接口中实现.

Interface in a simple and crude way is a bag of methods. You are simply telling the developer if you are going to use this implement these. These methods are not declared as abstract but cannot be implemented in the interface.

interface iA {
    public function doFoo();
    public function doBar();
}

一个接口可以被其他接口扩展,只是在接口中添加更多的方法

An interface can be extended by other interface which is just adding more methods to the interface

interface iB extends iA {
    public function doFooBar();
}

interface iC {
    public function doAnything();
}

并由类实现

class A implements iA{
    public function doFoo(){
        //Some Code
    }
    public function doBar(){
        //Some Code
    }
}

class B implements iB{
    public function doFoo(){
        //Some Code
    }
    public function doBar(){
        //Some Code
    }
    public function doFooBar(){
        //Some Code
    }   
}

接口的附加优点是一个类或抽象可以实现多个

The added advantage of interfaces is that a class or abstract can implement more then one

abstract class C implements iA, iC {
    public function doFoo(){
        //Some Code
    }
}

class D extends C {
    //get doFoo() from C implementation and must implement the remaining...
    public function doBar(){
        //Some Code
    }
    public function doAnything(){
        //Some Code
    }
}

这篇关于PHP 类中的多级继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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