如果我在php中使用抽象类有什么好处?

发布于 2024-11-16 03:47:00 字数 249 浏览 0 评论 0原文

可能的重复:
接口与抽象类

如果我使用有什么优势 PHP 中的抽象类?

如果我使用摘要的目的是什么 类还是接口?

两者都只是创造防守 没有身体的名字

Possible Duplicate:
interface vs abstract class

What are the advantage if i use
abstract class in php?

What is the aim if i use abstract
class or interfaces?

Both are simply creating defenition
names with out body

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

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

发布评论

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

评论(4

看透却不说透 2024-11-23 03:47:00

在 php 中使用抽象类有什么好处?我找不到任何好的东西。我认为我可以轻松地完成所有工作而不使用抽象类?

你当然可以。但是,如果有许多类型几乎相同的对象,则可能有助于将通用功能提取到“基”类中,这意味着您不必重复该逻辑。

但实际上有两个原因。对我来说,第一个原因是抽象类的所有后代都具有相同类型,并且都遵循完全相同的接口。这意味着 PDF 文档将具有与 docx 文档相同的接口,并且客户端代码不必关心它正在处理哪个对象。简短示例(PHP 语言)。

<?php
abstract class Document {
    protected $author;

    public function __construct( $author ) {
        $this->author = $author;
    }

    abstract public function render( );

    public function author( ) {
        return $this->author;
    }
}

class PdfDocument extends Document {
    public function render( ) {
        // do something PDF specific here.
    }
}

class DocxDocument extends Document {
    public function render( ) {
        // do something DOCX specific here.
    }
}


class DocumentHandler {
    public function handle( Document $document ) {
        $this->log( 'Author has been read ' . $document->author( ) );
        return $document->render( );
    }
}

首先;请注意 DocumentHandler 类不知道它实际处理的文档类型。它甚至不在乎。这是无知的。但是,它确实知道可以调用哪些方法,因为两种类型的文档之间的接口是相同的。这称为多态性,并且可以通过实现 Document 接口轻松实现。

第二部分是;如果每个文档都有一个作者,并且总是要求该作者,您可以将该方法复制到 PdfDocument 和 DocxDocument,但您会重复自己。例如,如果您决定希望作者用大写字母书写,并且您将 return $this->author 更改为 ucwords( $this->author ),则必须执行此操作的次数你已经复制了那个方法。使用抽象类,您可以定义行为,同时将类本身标记为不完整。这非常方便。

What are the advantage if i use abstract class in php? i cant find anything good on that. I think i can easily do all work with out using the abstract class?

You could, naturally. However, if there are many objects that are of pretty much the same type, it might help to extract common functionality out into a "base" class, which means you don't have to duplicate that logic.

There are actually two reasons though. The first reason, for me, would be that all descendants of your abstract class have the same type, and both adhere to the exact same interface. That means that a PDF document for example will have the same interface as a docx document, and the client code doesn't have to care which object it's handling. Short example (in PHP).

<?php
abstract class Document {
    protected $author;

    public function __construct( $author ) {
        $this->author = $author;
    }

    abstract public function render( );

    public function author( ) {
        return $this->author;
    }
}

class PdfDocument extends Document {
    public function render( ) {
        // do something PDF specific here.
    }
}

class DocxDocument extends Document {
    public function render( ) {
        // do something DOCX specific here.
    }
}


class DocumentHandler {
    public function handle( Document $document ) {
        $this->log( 'Author has been read ' . $document->author( ) );
        return $document->render( );
    }
}

First of all; mind the fact that the DocumentHandler class has no knowledge of which type of Document it's actually handling. It doesn't even care. It's ignorant. However, it does know which methods can be called upon, because the interface between the two types of Documents are the same. This is called polymorphism, and could just as easily be achieved with the implementation of a Document interface.

The second part is; if each and every document has an author, and that author is always requested, you could copy the method over to the PdfDocument as well as the DocxDocument, but you'd be duplicating yourself. For instance, if you decide that you want the author to written with capitals, and you'd change return $this->author to ucwords( $this->author ), you'd have to do it as many times as you've copied that method. Using an abstract class, you can define behaviour, while marking the class itself as incomplete. This comes in very handy.

笛声青案梦长安 2024-11-23 03:47:00

当您有许多具有相同方法的类时,抽象类会为您提供帮助。

示例:

abstract class Foo {
  public function foo1() {
    //Do something
  }

  public abstract class foo2();
}

class Bar extends Foo {
  public class foo2() {
    //Do something
  }
}

class Baz extends Foo {
}

将会发生什么:

  • 您不能使用 new Foo();Foo 是抽象的。
  • 您将能够使用 Bar.foo1()Baz.foo1(),它们会做同样的事情。
  • 您将会遇到错误,因为 Baz 没有实现抽象方法 foo2

它有用的示例:

abstract class Shape {
  public function display() { /* ... */ }
  //...
}

class Circle extends Shape {
  //...
}

class Rectangle extends Shape {
  //...
}

//...

您希望每个类都能够 display(),但本身不存在“Shape”这样的东西。

Abstract classes help you when you have many classes that have the same methods.

Example:

abstract class Foo {
  public function foo1() {
    //Do something
  }

  public abstract class foo2();
}

class Bar extends Foo {
  public class foo2() {
    //Do something
  }
}

class Baz extends Foo {
}

What will happen:

  • You can't use new Foo();, Foo is abstract.
  • You will be able to use Bar.foo1() and Baz.foo1(), they will do the same thing.
  • You will have an error, because Baz doesn't implement the abstact method foo2.

Example where it is usefull:

abstract class Shape {
  public function display() { /* ... */ }
  //...
}

class Circle extends Shape {
  //...
}

class Rectangle extends Shape {
  //...
}

//...

You want every class to be able to display(), but there is no such thing as "Shape" by itself.

葬花如无物 2024-11-23 03:47:00

并非所有抽象类的方法都必须为空,可能有一些基本方法(和属性)可以使用。例如 - 您有一个电子商店,并且您开发了一个抽象类来导入产品。该类有一个将产品保存到数据库的方法,生成产品的 url 和一个从某处检索产品的抽象方法(因此必须在扩展类中实现)。
接口只有空白方法,没有属性(但可以有常量),因此可能没有实际逻辑,只有方法常量、方法名称及其访问修饰符。

Not all of the abstract class' methods must be empty, there may be some basic methods (and properties) to work with. For example - you have and e-shop and you develop an abstract class to import products. The class has a metod to save products to db, to generate product's url and an abstract method to retrieve products from somewhere (which therefore has to be implemented in the extended class).
Interface only has blank methods and no properties (can have constants though), so tere may be no actual logic, just method constants, method names and their access modifiers.

中二柚 2024-11-23 03:47:00

顾名思义,接口的目的是显式声明类和实例提供的数据和方法的接口,而不需要立即对这些方法进行编码。典型的例子是几何形状。这样的接口可以定义一个产生这种形状的面积的方法:

interface Shape {
    public function getArea();
}

可能有许多不同的类实现这个接口,例如CircleSquare,它们将提供不同的实现对于方法getArea()。然后,您可以实现一个显示任何几何形状信息的函数:

function displayInformation(Shape $shape) {
    echo "The area of this shape is: " . $shape->getArea();
}

您可以将实现 Shape 接口的任何对象传递给此函数,并且该接口保证 getArea()方法存在。

当然,这些概念在比 PHP 强类型的编程语言中可能更有用。

As the name suggests, the purpose of an interface is to explicitly declare the interface to data and methods provided by a class and the instances, without the need to code those methods right away. The classic example is that of geometric shapes. Such an interface could define a method that produces the area of such a shape:

interface Shape {
    public function getArea();
}

There could be a number of different classes implementing this interface like Circle and Square that would provide different implementations for the method getArea(). You could then implement a function that displays information on any geometric shape:

function displayInformation(Shape $shape) {
    echo "The area of this shape is: " . $shape->getArea();
}

You could pass any object implemented the Shape interface to this function, and the interface guarantees that the getArea() method is present.

Of course, these concepts might be more useful in programming languages that are more strongly typed than PHP.

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