简体   繁体   English

PHP最佳实践:使用混合返回类型

[英]PHP Best Practice: Using Mixed return types

I understand that this topic was briefly approached here but I hope to understand the general do's and don'ts of using multiple return types in PHP. 我理解这个主题在这里简要介绍了,但我希望了解在PHP中使用多个返回类型的一般做法和不做。 There seems to be varied opinions about this feature of PHP. 关于PHP的这个功能似乎有不同的意见。

I would tend to agree that, as pointed out in the thread linked above, for errors, using exceptions is probably more suited; 我倾向于同意,正如上面链接的线程所指出的,对于错误,使用异常可能更合适; But, what about a function returning, say, two types of meaningful values? 但是,如果函数返回两类有意义的值呢? For example, let's say a function that returns all the lights that are OFF in a house (:) yes, I'm winging it!) 例如,假设一个函数返回一个房子里关闭的所有灯(:)是的,我正在甩它!)

Essentially, this is what I want to convey: If a basic condition is not met and I don't see a point in going forward and computing my list, I return a boolean. 从本质上讲,这是我想要传达的:如果基本条件没有达到并且我没有看到前进和计算我的列表的一点,我返回一个布尔值。 Otherwise, go ahead and do so: 否则,请继续这样做:

    public function getLightsThatAreOff($house) 
    {
        // if $house itself does not have any power, return true 
        // else 
              //compile an array of lights that are off and return then
    }

I would think that the use case mentioned above is a sort of a sweet-spot for multiple return types. 我认为上面提到的用例对于多种返回类型来说是一种甜蜜点。 In either case, I would appreciate it if someone provides a general set of guidelines on how to determine whether to use this feature or not. 在任何一种情况下,如果有人提供了一套关于如何确定是否使用此功能的一般指南,我将不胜感激。

Thanks! 谢谢!

Mixed types are not good, even if documented well. 即使记录良好,混合类型也不好。 For example, if a method is returning an array, but there is nothing to return, then it should be an empty array, not false or anything else. 例如,如果一个方法返回一个数组,但没有什么可以返回,那么它应该是一个空数组,而不是false或其他任何东西。 ie

array()

It really depends on how you intend to use these functions. 这实际上取决于您打算如何使用这些功能。 Even if you're part of a large development team, as long as you document your code effectively, then you should be able to safely return mixed types. 即使您是大型开发团队的一员,只要您有效地记录代码,那么您应该能够安全地返回混合类型。 If you develop for yourself, then it really doesn't matter, as long as you know how and where to use your functions. 如果你为自己开发,那么只要你知道如何以及在何处使用你的功能它就没关系。

I will say though, from personal experience, that whenever you can alleviate the possibility of an error, you should. 不过,从个人经验来看,我会说,无论什么时候你可以减轻错误的可能性,你都应该这样做。 Meaning, if the expected return value of a function is an array, you should probably return a blank array instead of a boolean (false). 意思是,如果函数的预期返回值是数组,则应该返回空数组而不是布尔值(false)。 That way, even if someone doesn't read your documentation, the worst that can happen is that when they try to loop through the array, nothing happens, and the script continues. 这样,即使有人没有阅读你的文档,最糟糕的情况是,当他们试图遍历数组时,没有任何反应,脚本继续。 If you can't foresee it causing any issues, I would highly recommend doing it this way instead of simply returning false. 如果您无法预见它会导致任何问题,我强烈建议您这样做,而不是简单地返回false。 I can't even count how many times I've heard "your database class is broken" when I returned false instead of an array of results if the query failed. 我甚至无法计算当我返回false而不是一个结果数组(如果查询失败)时,我听过“你的数据库类被破坏了多少次”。

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

相关问题 使用返回true或false的函数的PHP最佳实践是什么? - What is the PHP best practice for using functions that return true or false? WordPress:最佳实践,如果将html / php混合到帖子中,则插入一个块 - Wordpress: best practice to insert a block if mixed html/php into a post 使用PHP存储和返回表单数据的最佳实践 - Best practice to store and return form data with PHP 将 JSON 发送到 php 并保留字段类型的最佳实践 - Best practice for sending JSON to php with keeping field types 使用PHP docblocks @return标记是否存在将布尔值返回为整数(即​​1或0)的最佳实践? - Using the PHP docblocks @return tag is there a best practice for returning a boolean as an integer (i.e. 1 or 0)? 使用 jquery 与 php 类交互的最佳实践? - Best practice for using jquery to interact with php classes? 使用Cookie进行PHP身份验证的最佳做法是什么? - What is the best practice for using Cookies for authentication with PHP? 使用PHP录制页面点击的最佳做法? - Best practice for recording page hits using PHP? 使用php在哪里存储信息-最佳实践 - Where to store information using php - Best Practice 使用PHP + Phonegap进行用户注册的最佳实践 - Best Practice for User Registration using PHP + Phonegap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM