多功能是PHP中的一大功能吗? [英] multiple functions is one big function in PHP?

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

问题描述

所以我正在编写 DataBase 类,它将是 PHP Controller 和<$ c $之间的封装层c> MySQL View 。

so I'm writing DataBase class which will be an encapsulation layer between PHP Controller and MySQL View.

interface iDataBase {
    public function drug($action, $drug);
    public function company($action, $company);
    public function activeIngredient($action, $activeIngredient);
}

起初,我想到了将所有设置方法和获取方法分开,例如getAllDrugs(), updateDrug(),removeDrug(),getForUpdate(),getDrug()等,但是后来我意识到我正在污染具有太多功能的数据库接口,而且这是一个非常小规模的版本,我正在考虑增加很多-更多的类和更多的功能。因此,不用使用我只是设置3的很多功能。 $ action 将确定用户希望对某些类进行哪种操作。因此,现在,可能的操作如下: add, getAll, getForUpdate, update, remove

At First I thought of making all setters and getters seperate like getAllDrugs(), updateDrug(), removeDrug(), getForUpdate(), getDrug() and so one, but then I realised that I was polluting database interface with too much functions, plus this is a very small-scale version, I'm considering adding much-more classes and much more functionality. So, instead of using a lot of function I just setteled for 3. $action will determine what kind of thing does user want to do with certain class. so, for now, possible actions are these: "add", "getAll", "getForUpdate", "update", "remove"

,但是被 $ action 掩盖的这些函数有不同的操作,因此它们的返回结果不同,第二个参数也可能不同。

but these functions masked by $action have different things to do, so their their return result is different and second argument can also be different.

我的解决方案是一种好的做法吗?我敢肯定你们中的许多人都有同样的问题,您是如何解决的?

Is my solution a good practice? I'm sure many of you had the same problem, how did you solve it? Are there any possible problems?

P.S。 Drug,Company,ActiveIngredient 都是所有类

P.S. Drug, Company, ActiveIngredient are all classes

推荐答案

一个函数应该具有定义明确,职责范围狭窄,定义明确的最低回报类型。如果您开始创建可以执行所有操作的上帝功能,并且根据传递的参数来确定厨房的水槽,那么您将陷入难以维护意粉代码的境地。您不希望一个函数在传递X时执行A并返回B,但是如果传递Y则返回C并返回D等等。

A function should have clearly defined, narrow responsibilities with clearly defined, minimalist return types. If you start to create "god functions" which do everything and the kitchen sink depending on what arguments you pass, you're going heavily into the territory of hard to maintain spaghetti code. You do not want a function that does A and returns B if you pass it X, but does C and returns D if you pass it Y etc...

当您看到类似的模式时,开始 concrete 并随时间进行概括是一个好主意。因此,创建您实际需要的方法:

It is a good idea to start concrete and generalize over time as you see similar patterns emerge. So, create the methods you actually need:

public function findUserById($id)
public function findUserByEmail($email)
public function updateCompanyName($id, $newName)

如果发现已共享这些函数之间的代码,统一后台代码以使其保持DRY:

If you find you have shared code between these functions, unify the code behind the scenes to keep it DRY:

public function findUserById($id) {
    return $this->find('SELECT * FROM user WHERE id = ?', $id);
}

public function findUserByEmail($email) {
    return $this->find('SELECT * FROM user WHERE email = ?', $email);
}

protected function find($query, $arg) {
    ...
}

不要以另一种方式开始,认为您只需要X,Y和Z似乎很相似,可以统一为一个方法,然后再找出X,Y和Z之间有一些细微的差别,并且在每种情况下都用特殊情况填充代码。这只会导致功能过大或过大而根本无法自行执行。

Don't start the other way around, thinking you "only need X,Y and Z" which seem similar enough to be unified into one method, then later finding out there are small differences between X, Y and Z and littering your code with special cases for each. That just leads to functions which are either ginormous or so general they basically do nothing on their own.

这篇关于多功能是PHP中的一大功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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