预先声明所有私有/局部变量? [英] Pre-declare all private/local variables?

查看:60
本文介绍了预先声明所有私有/局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个基本问题,但现在让我一直想知道.

This may be a basic question, but it has kept me wondering for quite some time now.

我应该将所有私有/局部变量都声明为私有吗?还是仅对重要"变量才需要?

Should I declare all private/local variables being private? Or is this only necessary for "important" variables?

例如,我有一个(临时)计算结果.我应该预先声明这个变量吗?

For instance, I have the (temporary) result of a calculation. Should I pre-declare this variable?

希望有人可以指出这一点.

Hope someone can point this out.

推荐答案

对于变量在使用之前定义和初始化它们通常是一个很好的经验法则.这不仅包括定义和初始值,还包括对输入值的验证和过滤,以便在对这些变量所包含的数据进行具体处理之前,先建立代码块的所有前提条件.

It's generally a good rule of thumb for variables to define and initialize them before use. That includes not only definition and initial value but also validation and filtering of input values so that all pre-conditions a chunk of code is based on are established before the concrete processing of the data those variables contain.

对象成员(属性)自然是相同的,因为它们是整个对象的变量.因此,它们应该已经在类中定义了(默认情况下,它们的值在PHP中为NULL).动态值/过滤可以在构造函数和/或设置方法中完成.

Same naturally applies to object members (properties) as those are the variables of the whole object. So they should be defined in the class already (by default their value is NULL in PHP). Dynamic values / filtering can be done in the constructor and/or setter methods.

可见性规则类似于代码中的任何规则:尽可能少(很难实现的简单规则).因此,请保持本地状态,然后将其保持私有状态-取决于它是函数变量还是对象属性.

The rule for visibility is similar to any rule in code: as little as necessary (the easy rule that is so hard to achieve). So keep things local, then private - depending if it's a function variable or an object property.

也许请记住,在PHP中,您可以从同一类(不仅是同一对象)内访问私有属性.知道这一点可能很有用,因为它使您可以将内容保密的时间更长一些.

And perhaps keep in the back of your mind that in PHP you can access private properties from within the same class - not only the same object. This can be useful to know because it allows you to keep things private a little bit longer.

例如,我有一个(临时)计算结果.我应该预先声明这个变量吗?

For instance, I have the (temporary) result of a calculation. Should I pre-declare this variable?

这通常是函数或方法中的局部变量.它在接收到计算方法的返回值时定义.因此,无需(预先)预先声明它.

This is normally a local variable in a function or method. It's defined when it receives the return value of the calculation method. So there is no need to pre-declare it (per-se).

...

function hasCalculation() {
    $temp = $this->calculate();
    return (bool) $temp;
}

...

如果计算很昂贵,则可以存储(缓存)该值.当您将其封装在一个对象中时,这很容易工作.在这种情况下,您将使用私有属性来存储计算后的值.

If the calculation is/was expensive it may make sense to store (cache) the value. That works easily when you encapsulate that, for example within an object. In that case you'll use a private property to store that value once calculated.

把这些规则放在一小撮盐上,它们是用于一般的方向,您可以轻松地对其进行修改,因此这是可以扩展的,因此是使事情保持灵活性的好方法.

Take these rule with a grain of salt, they are for general orientation, you can easily modify from that, so this is open to extend, so a good way to keep things flexible.

这篇关于预先声明所有私有/局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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