gpt4 book ai didi

php - 在 PHP 中设计服务层类

转载 作者:IT王子 更新时间:2023-10-29 00:03:42 24 4
gpt4 key购买 nike

我是 recently introduced通过 Jani Hartikainen 服务图层在有关如何最好地处理 MVC 应用程序中的表单数据的讨论中。在doing some reading之后我真的可以看到这种方法的好处。我的问题是:

服务类应该如何构建?

  • 首先,user_service() 是适合我的 user() 模型的类名还是有其他标准?
  • 由于我的服务中的方法只会执行一项任务,因此认为这些方法可以始终是静态函数 是否正确?服务类不代表数据,而是一系列操作,因此这似乎很合适。
  • 服务方法是否应该只接受一个参数,即数组

考虑一个表单已将数据发布到 Controller 以保存用户数据:

<?php

class form_controller extends controller
{

public function process_submit()
{
if(user_service::update_preferences($_POST))
{

echo json_encode(array('success' => true));
}
else
{
echo json_encode(array('success' => false));
}
}

}

class user_service
{

// Accepts array()
public static function update_preferences($fields)
{

// Check for required fields
if((
isset($fields['firstname']) and
isset($fields['lastname']) and
isset($fields['email'])
) == false
{
return false;
}

// Update user
try
{
$s = new user();
$s->set_firstname($fields['firstname']);
$s->set_lastname($fields['lastname']);
$s->set_email($fields['email']);
$s->update();

return true;
}
catch(Exception $e)
{
return false;
}
}
}

我觉得这是一个很好的方法,因为:

  • 我可以在表单中添加另一个字段,而不必更新 controller,只需更新 service。 Controller 不应该关心传递的是什么数据,而只关心传递的数据,这似乎是正确的。这使我的 Controller 和模型中的逻辑保持较小。
  • 如果我不传递数组,我可以设置具有多个参数的函数。例如,我的函数可以是 update_preferences($firstname, $lastname, $email)。然而,这可能会产生超过 20 个参数的函数(对于大型表单),而且顺序会变得很难处理。
  • 我可以传递一个对象,但这有意义吗?如果我正在创建一个对象,它应该是它所代表的对象(在这种情况下是用户),对吗?但是 Controller 实例化用户对象有意义吗?这首先不是服务层的重点吗?
  • 也许有一些方法有多个参数(当只有一到三个参数时)和一些方法接受数组(当有很多字段时)。这看起来就像一场噩梦,因为您总是必须引用该类才能知道该特定方法要求什么。

有人对在这里做什么是正确的有意见吗?我在正确的轨道上吗?你过去做过什么?非常感谢!

最佳答案

不妨回答这个问题,因为你甚至给我发了一封电子邮件 ;)

First, is user_service() an appropriate class name for my user() model or is there another standard?

这是可以接受的。但是,您应该使用已建立的 PHP 编码约定之一,例如 PEAR 或 ZF 约定。在这两种情况下,类名都是 UpperCamelCase,方法名是 lowerCamelCase。使用它,类将是 UserUserService

Since the methods in my service will only be doing one task, is it correct to think that these can always be a static function? A service class isn't representing data, but rather is a series a actions, so this seems appropriate.

没有。将方法设为静态是一个糟糕的设计选择——这适用于大多数代码,而不仅仅是服务。使用服务的主要原因之一是,通常您的服务需要与数据存储或代表数据层(存储库、数据访问对象等)的另一个类进行交互。

当您的服务有静态方法时,这意味着您需要在您的方法中实例化您的依赖项。这反过来意味着,除其他外,代码变得难以测试,因为您无法轻松替换依赖项。

有一些很好的读物,例如 here (事实上​​ ,该博客上的几乎所有内容都适合软件开发人员阅读)

Should a service method only accept one argument, which would be an array?

这取决于方法的作用。假设您处理表单结果集的示例,那么是的,这可能会起作用。在其他一些情况下,这可能是一个糟糕的选择。

I can add another field to my form and I won't have to update the controller, just the service. [ ... ]

If I didn't pass an array, I could setup functions with multiple arguments. [ ... ]

是的,在我看来,您对这两个案例的论证对于这个用例来说非常准确。

I could pass an object, but does that make sense? If I'm creating an object, it should the be the object it represents (the user in this case) right? But does it make sense that the controller instantiates the user object? Isn't that the whole point of the service layer in the first place?

这取决于。例如,如果您使用的框架允许您将表单表示为对象(例如 Zend Framework 和 Zend_Form ),您可以考虑将表单对象直接传递给服务。

Maybe there is an argument for having some methods with multiple arguments (when there are just one to three) and some methods that accept an array (when there are lots of fields). This just seems like it could be a nightmare, as you would always have to reference the class to know what that particular method was asking for.

您通常应该根据方法的名称使参数至少具有一半的可猜测性。在 something I work on ,我们有一个模型,其中包含例如企业和产品,其中企业可以赞助产品。在 ProductService 中,我们有一个名为 sponsorProduct 的方法,它以业务和产品作为参数。你几乎可以猜到它需要这两个(如果你熟悉代码库的话)

IDE 通常也会帮助您解决这个问题 - 它们提供代码帮助,显示参数函数采用的内容。这是我认为 IDE 在大型项目中非常有用的主要原因之一,在这些项目中,您无法始终记住某个函数究竟需要什么作为参数。

至于参数个数,我觉得通常你应该尽量有单独的参数。这使任何人都可以通过查看函数的签名轻松地了解需要哪些参数,并允许您非常轻松地定义类型提示和默认值。

然而,当您获得如此多的参数时,有时会觉得太多了。这可能在 +5 左右,具体取决于它是哪种方法。在这种情况下,您可以考虑使用数组或称为参数对象的东西,它本质上是一个包含调用的所有参数的对象。有关参数对象的更多信息 here

关于php - 在 PHP 中设计服务层类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6524037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com