PHP 中的类抽象和对象接口之间的区别?

发布于 2024-09-05 00:36:13 字数 776 浏览 9 评论 0 原文

类抽象PHP 中的对象接口?我问这个问题是因为,我真的不明白他们俩有什么意义,他们都做同样的事情!那么,同时使用两者来对抗其中一个或另一个的优点和缺点是什么?

类抽象

abstract class aClass
{
    // Force extending class to define these methods
    abstract public function setVariable($name, $var);
    abstract public function getHtml($template);
}

对象接口

interface iClass
{
    // Force impementing class to define these methods
    public function setVariable($name, $var);
    public function getHtml($template);
}

What is the difference between a Class Abstraction and an Object Interfaces in PHP? I ask because, I don't really see the point to both of them, they both do the same thing! So, what are the advantages of disadvantages using both against one or the other?

Class Abstraction:

abstract class aClass
{
    // Force extending class to define these methods
    abstract public function setVariable($name, $var);
    abstract public function getHtml($template);
}

Object Interface:

interface iClass
{
    // Force impementing class to define these methods
    public function setVariable($name, $var);
    public function getHtml($template);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亽野灬性zι浪 2024-09-12 00:36:13

您可以实现多个接口,但只能扩展一个类。

可能:

class MyClass extends MyAbstract implements MyInterface1, MyInterface2, MyInterface3 {  }

不可能:

class MyClass extends MyAbstract1, MyAbstract2 implements MyInterface {  }

此外,接口不能有实现。

可能:

abstract class MyClass {
    function doSomething() {
        echo "I can give this method an implementation as this is an abstract class";
    }
}

不可能:

interface MyClass {
    function doSomething() {
        echo "I can NOT give this method an implementation as this is an interface";
    }
}

来自 Java 术语表

接口允许某人从头开始实现您的接口或在其他代码中实现您的接口,而这些代码的原始或主要目的与您的接口有很大不同。对于他们来说,您的界面只是偶然的,必须添加到他们的代码中才能使用您的包。

相比之下,抽象类提供了更多的结构。它通常定义一些默认实现并提供一些对完整实现有用的工具。问题是,使用它的代码必须使用您的类作为基础。如果想要使用您的包的其他程序员已经独立开发了自己的类层次结构,这可能会非常不方便。在Java中,一个类只能继承一个基类。

您还应该看看这个问题 - Damien 关于接口如何是一个合同是一项重要的合同。

You can implement multiple interfaces but only extend one class.

Possible:

class MyClass extends MyAbstract implements MyInterface1, MyInterface2, MyInterface3 {  }

Not Possible:

class MyClass extends MyAbstract1, MyAbstract2 implements MyInterface {  }

Additionally, an interface cannot have implementation.

Possible:

abstract class MyClass {
    function doSomething() {
        echo "I can give this method an implementation as this is an abstract class";
    }
}

Not Possible:

interface MyClass {
    function doSomething() {
        echo "I can NOT give this method an implementation as this is an interface";
    }
}

From the Java Glossary:

An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them, your interface is only incidental, something that have to add on to the their code to be able to use your package.

An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.

You should also take a peek at this question - Damien's point about how an interface is a contract is an important one.

掩饰不了的爱 2024-09-12 00:36:13

除了方法定义之外,抽象类还可以定义方法实现。 接口只能指定方法定义。

An abstract class can define method implementation, in addition to method definitions. An inferface can only specify the method definition.

小鸟爱天空丶 2024-09-12 00:36:13

多个接口加上您的脚本不会遇到任何家长的困惑。

Multiple interfaces plus your scripts don't run into any parent confusion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文