简体   繁体   English

PHP最佳实践:给定参数是否应始终具有一致类型?

[英]PHP Best Practices: Should a given parameter always have a consistent type?

I have a function that accepts a checkGlossary bool param as well as an optional glossary array. 我有一个函数接受checkGlossary bool param以及一个可选的glossary数组。
Their states are directly tied together. 他们的州直接捆绑在一起。
The glossary is never required if the bool is FALSE, and inversely, it is always required if the bool is TRUE. 如果bool为FALSE,则永远不需要词汇表,反之,如果bool为TRUE则始终需要词汇表。

To me, it seems like this could easily be simplified from: 对我来说,似乎可以很容易地简化:

// Current
function doSomething($param1, $param2, $checkGlossary=FALSE, $glossary=NULL){
    // blah blah blah
    if($checkGlossary)
        array_search($glossary[$param2]);
    // etc etc etc
}

... to: ... 至:

// Proposed
function doSomething($param1, $param2, $glossary=FALSE){
    // blah blah blah
    if($glossary)
        array_search($glossary[$param2]);
    // etc etc etc
}


My only hesitation is over the fact that the type for $glossary ( bool or array ) would be unpredictable. 我唯一的犹豫是因为$glossaryboolarray )的类型是不可预测的。
It doesn't bother me as long as I'm not going against some Best-Practices guidelines. 只要我不违反一些最佳实践指南,它就不会打扰我。

Thoughts? 思考?

您可以使用is_array()is_bool()来检查其类型!

It is always a bad idea to have function parameters with what PHP calls mixed datatype. 使用PHP调用mixed数据类型的函数参数总是一个坏主意。 It requires additional code in the function to check the type of the parameter and obviously it can get very confusing. 它需要函数中的附加代码来检查参数的类型,显然它会变得非常混乱。

Probably the easiest solution in your special case would be to use the array length as indicator of whether to use the glossary code. 在您的特殊情况下,最简单的解决方案可能是使用数组长度作为是否使用词汇表代码的指示器。 You need a way to declare that the glossary array should not be used. 您需要一种方法来声明不应使用词汇表数组。 So you should ask yourself: When is it pointless to use the glossary? 所以你应该问自己:什么时候使用术语表是没有意义的? When it's empty of course. 当它是空的当然。 So, I would suggest that you get rid of the flag and define array() as default for your other parameter: 所以,我建议您摆脱该标志并将array()定义为其他参数的默认值:

function doSomething($param1, $param2, $glossary=array()) {
    if (count($array) > 0) {
        // do your glossary code here
    }
    // all the other stuff goes here
}

To me this seems semantically correct and works just fine. 对我来说,这似乎在语义上是正确的,并且工作正常。

I don't know what exactly you are building there, but another solution would be to put it all into a class and have the glossary as instance variable. 我不知道你在那里构建了什么,但另一种解决方案是将它全部放入一个类中并将词汇表作为实例变量。 In case you could use the glossary in several function calls. 如果您可以在多个函数调用中使用词汇表。 It would roughly look like this: 它大概如下:

 public class SomeAccurateClassName {
     private $glossary = array();

     function setGlossary(array $glossary) {
         $this->glossary = $glossary;
     }

     function doSomething($param1, $param2) {
         if (count($array) > 0) {
             // do your glossary code here
         }
         // all the other stuff goes here
     }
 }

Considering that you basically have a state (use glossary or don't use glossary) it might be a good idea to encapsulate that withint a class. 考虑到你基本上有一个状态(使用词汇表或不使用词汇表),将一个类封装起来可能是一个好主意。

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

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