什么时候应该在 PHP 类中声明变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1086494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:54:08  来源:igfitidea点击:

When should I declare variables in a PHP class?

phpoop

提问by Andrew

I'm new to the OOP paradigm, so there's probably a simple explanation for this question...

我是 OOP 范式的新手,所以这个问题可能有一个简单的解释......

Do you always need to declare public object-wide variables in a class? For example:

您是否总是需要在类中声明公共对象范围的变量?例如:

<?php

class TestClass
{
    var $declaredVar;

    function __construct()
    {
        $this->declaredVar = "I am a declared variable.";
        $this->undeclaredVar = "I wasn't declared, but I still work.";
    }

    function display()
    {
        echo $this->declaredVar . "<br />";
        echo $this->undeclaredVar;
        echo "<br /><br />"; 
    }
}

$test = new TestClass;
$test->display();

$test->declaredVar = "The declared variable was changed.";
$test->undeclaredVar = "The undeclared variable was changed.";

$test->display();

?>

In this code, even though $declaredVaris the only declared variable, $undeclaredVaris just as accessible and useable--it seems to act as if I had declared it as public.

在这段代码中,即使$declaredVar是唯一声明的变量,$undeclaredVar也同样可访问和可用——它似乎表现得好像我已将其声明为公共变量。

If undeclared class variables are always accessible like that, what's the point of declaring them all up front?

如果未声明的类变量总是可以这样访问,那么预先声明它们有什么意义呢?

采纳答案by Federico klez Culloca

That variable isn't uninitialized, it's just undeclared.

该变量并非未初始化,只是未声明。

Declaring variables in a class definition is a point of style for readability. Plus you can set accessibility (private or public).

在类定义中声明变量是一种可读性的风格点。此外,您可以设置可访问性(私人或公共)。

Anyway, declaring variables explicitly has nothing to do with OOP, it's programming-language-specific. In Java you can't do that because variables must be declared explicitly.

无论如何,显式声明变量与 OOP 无关,它是特定于编程语言的。在 Java 中你不能这样做,因为变量必须显式声明。

回答by Dave Archer

If you declare a member inside the class you can set its accessibility e.g

如果你在类中声明一个成员,你可以设置它的可访问性,例如

private $varname;

回答by Michael Moussa

You should always declare your member variables and specify their accessibility within your classes. I like to put this information at the end of the class after my functions.

您应该始终声明您的成员变量并在您的类中指定它们的可访问性。我喜欢在我的函数之后把这些信息放在课程的最后。

You should define them as soon as you have enough information to do so. Possibly in the constructor or via setter functions.

一旦你有足够的信息来定义它们,你就应该定义它们。可能在构造函数中或通过 setter 函数。

It is important to do this because it makes life much easier for people working with your code. They don't have to guess where different properties are coming from or why they're there. Also, most (if not all) IDEs will not pick up on class variables unless you've declared them somewhere. Code completion/hints are one of the many benefits of IDEs and without declaring your variables, you will render that functionality useless.

这样做很重要,因为它使使用您的代码的人的生活变得更加轻松。他们不必猜测不同属性的来源或它们为何存在。此外,大多数(如果不是全部)IDE 不会接收类变量,除非您已在某处声明它们。代码完成/提示是 IDE 的众多优势之一,如果不声明变量,您将无法使用该功能。

回答by Artem Barger

General OOP paradigm of encapsulation says you should not expose your inner state variables out side that means they should be private, that allows you to change an implementation of your class without need to change the code where you make use of it. It's better practice to initialize variables via constructors and getters and setters method of the class.

封装的一般 OOP 范式表示您不应将内部状态变量暴露在外面,这意味着它们应该是私有的,这允许您更改类的实现,而无需更改使用它的代码。通过类的构造函数和 getter 和 setter 方法初始化变量是更好的做法。

回答by PaulJWilliams

In general variables should be initialized as soon as you have enough info to do it properly.

一般而言,只要您有足够的信息来正确地进行初始化,就应该初始化变量。

If a class variable needs certain info to be sensibly initialized then that info should be passed to the constructor.

如果一个类变量需要某些信息被合理地初始化,那么应该将该信息传递给构造函数。

Using PHP's syntax to implicitly declare variables at the point of definition is, IMHO a surefire way to introduce bugs - if your class needs a variable then declare it, and use all of the information hiding that OOP affords you.

使用 PHP 的语法在定义点隐式声明变量是,恕我直言,这是引入错误的可靠方法 - 如果您的类需要一个变量,然后声明它,并使用 OOP 为您提供的所有隐藏信息。

回答by Sajidur Rahman

As Federico Culloca said "That variable isn't uninitialized, it's just undeclared". Also you didn't define any access modifiers for them so that they behaving like public modifier applied to them.

正如 Federico Culloca 所说:“该变量并非未初始化,只是未声明”。此外,您没有为它们定义任何访问修饰符,因此它们的行为就像应用于它们的 public 修饰符。

You may already have known. PHP is a loosely typed language. But a programmer should always follow the best practices (but not to use primitive data types in php).

你可能已经知道了。PHP 是一种松散类型的语言。但是程序员应该始终遵循最佳实践(但不要在 php 中使用原始数据类型)。

You should use private modifier for class level variables and provide accessor and mutator methods (Getters and Setters) for them.

您应该对类级变量使用私有修饰符,并为它们提供访问器和修改器方法(Getter 和 Setter)。