PHP-OOP扩展了两个类? [英] PHP-OOP extending two classes?

查看:70
本文介绍了PHP-OOP扩展了两个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OOP的初学者,现在我正在尝试编写一些PHP类来与FTP服务器连接.

I am very beginner to OOP and now I am trying to write some PHP class to connect with FTP server.

class ftpConnect {
  private $server;
  private $user;
  private $password;

  private $connection_id;
  private $connection_correct = false;

  public function __construct($server, $user = "anonymous", $password = "anonymous@mail.com") {

    $this->server   = $server;
    $this->user     = $user;
    $this->password = $password;

    $this->connection_id      = ftp_connect($this->server);
    $this->connection_correct = ftp_login($this->connection_id, $this->user, $this->password);

    if ( (!$this->connection_id) || (!$this->connection_correct) ){
        echo "Error! Couldn't connect to $this->server";
        var_dump($this->connection_id);
        var_dump($this->connection_correct);
        return false;
    } else {
        echo "Successfully connected to $this->server, user: $this->user";
        $this->connection_correct = true;
        return true;
    }
  }
}

我认为目前该班级的人数微不足道.

I reckon that body of the class is insignificant at the moment.

主要问题是我在理解OOP想法时遇到了一些问题.

Main issue is that I have some problems with understanding OOP idea.

我想在每次运行代码时添加发送电子邮件.我已经下载了 PHPMailer类,并用它扩展了我的课程:

I wanted to add sending emails every time, when the code is run. I have downloaded PHPMailer Class and extended my class with it:

class ftpConnect extends PHPMailer {...}

我添加了一些变量和方法,到那时一切都按预期进行.

I have added some variables and methods and everything works as expected to that point.

我想:为什么不添加将所有内容存储在数据库中.每次用户运行以上代码时,应将适当的信息存储在数据库中.

I thought: why not to add storing everything in database. Everytime user runs above code, proper information should be stored in database.

我可以编辑我的ftpConnect class并添加连接到构造函数的数据库,以及一些其他方法来更新表.但是数据库连接和所有这些东西将来可能会被其他类使用,因此它绝对应该在单独的类中实现. 但是我的"main" ftpConnect class已经扩展了一个类,并且不能再扩展一个类.

I could edit my ftpConnect class and add database connecting to the constructor, and some other methods to updating tables. But database connecting and all that stuff could be used by other classes in the future, so it definitely should be implemented in seperate class. But my "main" ftpConnect class already extends one class and could not extend not a single one more.

我不知道如何解决此问题.也许我的ftpConnect class是复杂的,我应该以某种方式将其划分为几个较小的类吗? 非常感谢您的帮助.

I have no idea how can I resolve this problem. Maybe my ftpConnect class is to complex and I should somehow divide it into couple smaller classes? Any help is much appreciated.

推荐答案

对于初学者来说,我认为您的课程存在设计缺陷.您的构造函数正在工作.那不是构造函数应该在适当的OOP中执行的操作.您的构造函数应该只设置属性,并且应该有一个单独的方法connect().

For starters I think you have a design flaw in your class. Your constructor is doing work. That's not what a constructor should do in proper OOP. Your constructor should just set the properties and you should have a separate method connect().

第二个ftpConnect绝不应该扩展PHPMailer.他们是完全不同的两件事.阅读 Liskov替代原则,它是

Second ftpConnect should never ever extend PHPMailer. They are two totally different things. Read about the Liskov substitution principle it is part of the SOLID principles.

如果您的班级需要对数据库做一些事情或需要发送邮件,则需要将这些实例注入班级而不是扩展它们.这称为依赖注入,这将使以后进行单元测试变得容易,因为您可以轻松地进行单元测试.使用模拟邮件类或模拟数据库类.

If your class needs to do something with a database or needs to send mails you need to inject those instances into your class instead of extending them. This is called dependency injection and this will make it easy to do unit tests later, because you can easily use a mock mailer class or a mock database class.

如果要发送邮件,具有数据库访问权限并使用FTP,则至少需要3个不同的(分隔的)类(可能需要更多的类才能为数据库做一些映射等).基本上,每个班级应该负一个责任,只有一个责任.这称为单一责任原则.

If you want to send mails, have database access and use FTP you would need at least 3 different (separated) classes (probably even more to do some mapping for the db etc). Basically every class should have one responsibility and one only. This is called the single responsibility principle.

有关一些常规参考,请参见:

For some general references see:

  • Guide: Writing Testable Code
  • Google Clean Code Talks

这篇关于PHP-OOP扩展了两个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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