返回PHP函数/方法的最佳实践 [英] Best practice for returning in PHP function/method

查看:209
本文介绍了返回PHP函数/方法的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构一个广泛的代码库超时。从长远来看,我们将在课堂上开发整个系统,但同时我正利用这个机会来改进我的PHP技能,并改进我们在几百个网站上使用的一些遗留代码。

我已经阅读了关于如何最好地从定制函数返回数据的冲突文章,通常辩论分为两类,那些关注最佳技术实践的人,以及那些关心易于阅读和呈现的人。





我有意见(详细阐述)你从自定义PHP函数返回时考虑的最佳实践。

我不确定以下哪一项作为更好的标准,可以使用这个基本的理论函数来举例;

方法a。



填充返回变量并在函数结尾处返回它:

 <?php 
function theoreticalFunction($ var)
{
$ return ='';
if($ something> $ somethingelse){
$ return = true;
} else {
$ return = false;
}
return $ return;
}
?>

方法b。

返回每个端点:$ b​​
$ b

 <?php 
function theoreticalFunction($ var)
{
if($ something> $ somethingelse){
return true;
} else {
return false;
}
}
?>

可能的重复可能是使用返回true或false的函数的PHP最佳做法是什么?然而,尽管上面是我的基本示例,但这并不仅限于真或假。



我已浏览了PSR指南,但没有看到任何内容(但我可能错过了所以请随时指点我参考PSR :))。



扩展原始问题:



用于根据预期的/期望的输出类型返回不同的方法吗?

此方法是否根据程序的用途面向对象的编程方法?正如这个问题所示,面向对象带来了自己的怪癖,进一步扩展了可能的格式化/表示选项 PHP中的返回方法的最佳实践



请在您的解释中尽量说清楚,我对为什么选择您喜欢的方法以及什么,如果有的话,使你选择了另一种方法。

解决方案

有人争论单个退出点 (最后只有一个 return )和其他争论失败/返回提前的其他人。这仅仅是个人意见和可读性/可理解性的问题。几乎没有任何客观的技术答案。

现实情况是,这根本不是可以用教条式规定的东西。有些算法可以更好地表达为A,而其他算法可以更好地表达为B。

在您的具体情况下,这两种算法都不是最好的。你的代码应该写成:

  return $ something> $ somethingelse; 

有希望成为例子,没有普遍适用的规则。 >

I am refactoring an extensive codebase overtime. In the long run we are going to develop the whole system in classes but in the mean time I am using the opportunity to refine my PHP skills and improve some of the legacy code we use across several hundred websites.

I have read conflicting articles over time about how best to return data from a custom function, generally the debate falls into two categories, those concerned about best technical practice and those concerned about ease of reading and presentation.

I am interesting in opinions (with elaboration) on what you consider best practice when returning from a custom PHP function.

I am undecided as to which of the following as a better standard to follow using this basic theoretical function for example;

Approach a.

Populating a return variable and returning it at the end of the function:

<?php
function theoreticalFunction( $var )
{
    $return = '';
    if( $something > $somethingelse ){
       $return = true;
    }else{
       $return = false;
    }
    return $return;
}
?>

Approach b.

Returning at each endpoint:

<?php
function theoreticalFunction( $var )
{
    if( $something > $somethingelse ){
       return true;
    }else{
       return false;
    }
}
?>

A possible duplicate could have been What is the PHP best practice for using functions that return true or false? however this is not limited to simply true or false despite my basic example above.

I have looked through the PSR guidelines but didn't see anything (but I may have missed it so please feel free to point me to PSR with reference :) ).

Extending the original question:

Is the method used to return different depending on the expected/desired output type?

Does this method change depending on the use of procedural or object oriented programming methods? As this question shows, object orientation brings in its own eccentricities to further extend the possible formatting/presentation options Best practices for returns methods in PHP

Please try to be clear in your explanations, I am interested in WHY you choose your preferred method and what, if anything, made you choose it over another method.

解决方案

There are people arguing for single exit points in functions (only one return at the end), and others that argue for fail/return early. It's simply a matter of opinion and readability/comprehensibility on a case-by-case basis. There is hardly any objective technical answer.

The reality is that it's simply not something that can be prescribed dogmatically. Some algorithms are better expressed as A and others work better as B.

In your specific case neither is "best"; your code should be written as:

return $something > $somethingelse;

That would hopefully serve as example that there's simply no such thing as a generally applicable rule.

这篇关于返回PHP函数/方法的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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