简体   繁体   English

将变量传递给经常更改参数列表的方法

[英]Passing variables to methods with often changing parameter list

I am using a MVC model in php.我在 php 中使用 MVC 模型。 The controller is sending data to the model and sometimes it expects something back.控制器正在向模型发送数据,有时它希望得到一些回复。 As I write my webpage I add utilities which needs more information from the model.在我编写网页时,我添加了需要从模型中获取更多信息的实用程序。 Mostly it makes controller pass more arguments to the model interface.大多数情况下,它使控制器向模型接口传递更多参数。 The problem is that model interface mostly only passes those variables to another methods, so the parameter list should be changed in a few places.问题是模型接口大多只将这些变量传递给其他方法,因此参数列表应该在几个地方更改。 This obviously is unacceptable so how should it be handled?这显然是不可接受的,那么应该如何处理呢? I think that passing the array of data isn't the best idea because it can make it uncontrollable.我认为传递数据数组不是最好的主意,因为它可能使其无法控制。 Anything could add anything to that.任何东西都可以添加任何东西。 Currently I am using another solution.目前我正在使用另一种解决方案。 I create special class to handle this.我创建了特殊的类来处理这个问题。 Every one of them derives from a class containing something like "getAsArray()" method.它们中的每一个都源自一个包含类似“getAsArray()”方法的类。 I think that creating a special class for nearly every controller need is a waste of time and resources so here is my question: What is the best solution to this problem?我认为为几乎每个控制器需要创建一个特殊的类是浪费时间和资源,所以我的问题是:这个问题的最佳解决方案是什么? What are the common ideas?有哪些常见的想法?

Controller控制器

 public function addUser(){
   $this->load->model("User"); 
   $username = $this->someWayOfGettingData("username");
   $email = $this->someWayOfGettingData("email");
   $password = $this->someWayOfGettingData("password");
   $this->User->insert($username, $email, $password);
}

Model模型

private function someMethodOperatingOnData($username, $email, $password){
   $answer = $this->queryAdd($username, $email, $password);
   return $answer;
}

public function insert($username, $email, $password){
   return $this->someMethodOperatingOnData($username, $email, $password); 
}

What's the problem: When I decide to add another information about user (eg gender) I have to update the list of parameters in有什么问题:当我决定添加有关用户的其他信息(例如性别)时,我必须更新参数列表

insert($username, $email, $password);
someMethodOperatingOnData($username, $email, $password);
queryAdd($username, $email, $password);

to:到:

insert($username, $email, $password, $gender);
someMethodOperatingOnData($username, $email, $password, $gender);
queryAdd($username, $email, $password, $gender);

So I have to update every function passing those variables.所以我必须更新每个传递这些变量的函数。 I could use an array:我可以使用一个数组:

insert($arrayOfData);
someMethodOperatingOnData($arrayOfData);
queryAdd($arrayOfData);

But anywhere anything can add or remove those variables and I don't want that to happed.但是任何地方都可以添加或删除这些变量,我不希望这种情况发生。

I can think of different strategies for this:我可以想到不同的策略:

1) As you are currently doing, create a custom class, instantiate it with your parameters, pass it over. 1)正如您目前所做的那样,创建一个自定义类,使用您的参数对其进行实例化,然后将其传递。 However, since parameter list keep changing, your plain-old-data classes are going to be moving targets, ie costly.然而,由于参数列表不断变化,您的普通旧数据类将成为移动目标,即成本高昂。

2) You can create a dictionary and put those parameters into this dictionary and pass it along. 2)您可以创建一个字典并将这些参数放入这个字典中并传递它。 It is an anti-pattern in the OO world, but it could be a reasonable solution for you.这是面向对象世界中的一种反模式,但对您来说可能是一个合理的解决方案。

3) In Django world, one of the best practices is to pass the request altogether. 3) 在 Django 世界中,最佳实践之一是完全传递请求。 This approach keeps your method signatures clean.这种方法使您的方法签名保持干净。 This is more favorable compared to #2 above.与上面的#2 相比,这更有利。

I hope it helps.我希望它有帮助。

EDIT I am not a PHP guy.编辑我不是一个 PHP 人。 Think about dictionaries as key value pairs.将字典视为键值对。 In PHP, arrays are actually key-value pairs.在 PHP 中,数组实际上是键值对。 So, you can use arrays in PHP lingo, or Dictionaries in other languages like Smalltalk.因此,您可以在 PHP 行话中使用数组,或在其他语言(如 Smalltalk)中使用 Dictionaries。 So this option still holds, although the terminology I picked was not 100% correct, the idea or principal is still the same.所以这个选项仍然成立,虽然我选择的术语不是 100% 正确的,但想法或原则仍然是一样的。

The following is an excerpt from the book named Two Scoops of Django section 9-2:以下是《Django 的两勺》一书第 9-2 节的摘录:

For many utility functions, we are taking an attribute or attributes from the django.http.HttpRequest (or HttpRequest for short) object and gathering data or per- forming operations.对于许多实用函数,我们从 django.http.HttpRequest(或简称为 HttpRequest)对象中获取一个或多个属性并收集数据或执行操作。 What we've found is by having the request object itself as a primary argument, we have simpler arguments on more methods.我们发现通过将请求对象本身作为主要参数,我们在更多方法上有更简单的参数。 is means less cognitive overload of managing function/method arguments: just pass in the HttpRequest object!是意味着管理函数/方法参数的认知过载更少:只需传入 HttpRequest 对象!

EDIT2编辑2

I have just seen the edit you made to your question.我刚刚看到你对你的问题所做的编辑。 So, from the example you gave, what seems to be happening is you have a user class.所以,从你给出的例子来看,似乎正在发生的是你有一个用户类。 As time passes by, there might be a need to add more properties to the user class.随着时间的推移,可能需要向用户类添加更多属性。 For example, 20 years ago, there was no such property as MobilePhone, but now it is almost mandatory.比如20年前,还没有MobilePhone这样的属性,但现在几乎是强制性的。 So, naturally the best way is to add that member property to your User class.因此,自然最好的方法是将该成员属性添加到您的 User 类中。 Instantiate it with proper data and pass the User object, not individual user properties Until you can instantiate a User Object, you can pass around the request Object as mentioned in #3, afterwards, you can use the User object.用适当的数据实例化它并传递用户对象,而不是单个用户属性,直到您可以实例化用户对象,您可以像#3 中提到的那样传递请求对象,之后,您可以使用用户对象。 I believe this is your best route.我相信这是你最好的路线。

Cheers,干杯,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM